Recall:   The C compiler is a 1-pass compiler
 

  • The C compiler is a 1-pass compiler

Example: the C compiler cannot compile this code:

  #include <stdio.h>

  int main( )
  {
     printf("%d\n", x); // Use x before it is defined
  }

  int x = 1234;
   

The C compiler does not have the data type information on x when it was used !

This problem was fixed using: a variable declaration


The same issue exists with function definitions and function calls (= using a function)

The function header convey data type information on the parameter variables

  • The 1-pass C compiler can compile this C program without error:

       float square(float x)  // Function header
       {
          return x*x;
       }
    
       int main( )
       {
          int a = 4, b;
    
          b = square(a);     // Call the function square( )
       } 

  • The function definition of square( ) will be processed first

    The C compiler will know the data type of the parameters and return value of square( ) when the C compiler translates the function call

DEMO: demo/C/set1/param2.c

The function header convey data type information on the parameter variables

  • The 1-pass C compiler cannot compile this C program:

       int main( )
       {
          int a = 4, b;
    
          b = square(a);     // Call the function square( )  ERROR 
       }
    
       float square(float x)  // Function header
       {
          return x*x;
       } 

  • The function call of square( ) will be processed first

    The C compiler will report a compile error because it does not know the data type of the parameters !

    • The reason is actually more complicated - explained in next set of slides

DEMO: demo/C/set1/param2r.c

Declaring a function in C

  • Syntax to declare a function in C:

     extern return-type FuncName( paramType1, paramType2, ... ) ; 

  • Example:

     extern float square( float ) ; // You don't need the variable name...

  • Notes:

    • The keyword extern is optional

    • The parameter variable names are also optional

    My favorite way to write a function declaration:

     float square( float x ) ; // My preferred syntax 

Declaring a function in C   Example

  • Previously: the following C program has compile errors due to unknown data types for the parameter variables and return value:

    
    
       int main( )
       {
          int a = 4, b;
    
          b = square(a);     // Call the function square( )
       }
    
       float square(float x)  // Function header
       {
          return x*x;
       } 

    The C compiler will report a compile error due to conflicting data types in square( )...

DEMO: demo/C/set1/param2r.c

Declaring a function in C   Example

  • We can use a function declaration to provide the data type information to the C compiler:

       float square(float);  // Declare function square()
    
       int main( )
       {
          int a = 4, b;
    
          b = square(a);     // Call the function square( )
       }
    
       float square(float x)  // Function header
       {
          return x*x;
       } 

    The program will now compile and run correctly.

DEMO: demo/C/set1/param2s.c