Defining functions (= methods) in C

  • Syntax to define a function in C:

       return-Type   functionName ( formal-parameters )
       {// function-body 
           local variables definitions
    
           statements
       } 

    Example: function that returns the square of a number

       float square( float x )                  
       {
          float r;         // Define a local variable         
    
          r = x * x;       // Statement
          return ( r );    // Return statement
       }

Calling a functin in C   (uses the same syntax as Java)

  • Syntax to call (= invoke) a function:

      // Invoking a void type function:
         
      functionName ( arguments )  
    
    
    // Invoking a non-void type function: var = functionName ( arguments )

  • Example:

       float a = 4.0, b;
    
       b = square( a );   // Same syntax as in Java !
    

Remember:   C is not object oriented

  • C is not an object oriented programming language

    Meaning:

      • C does not have instance methods

        (instance methods works with objects and always use a implicit parameter (this))

  • C's functions are similar to static methods in Java:

      • C functions do not have implicit parameters

        I.e.: all parameters of a C function are listed in the parameter list of the function