Data type information in a function definition

  • The function header in a function definition contain:

      • The data type information of each parameter variable of the function

      • The data type information of the return value of the function

  • Example:

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

  • The data type information in the function header is very important for the compiler !

Review:   Data type matching rule in assignment statements

  • Recall that in an assignment statement:

       variable = expression ;
    
          (1) Evaluate the expression on the RHS
          (2) Convert if necessary to the correct data type
          (3) Assign the result to the variable on the LHS 

  • Programming Language Principle:

       variable = expression ;
          ^            ^
          |            |
          +------------+
         Must be the same data type
    

  • The compiler uses the data type information to make sure it stores the correct data representation in every variable !!!

     

Data type matching rule in parameter passing

  • The same principle applies in parameter passing:

    • A pass-by-value parameter is always passed by assigning the value of the actual parameter variable to the formal parameter variable

  • When a value x is passed in a function call:

       func( x )    // Pass parameter x

    Then:

    • the data type of the value in x must be the same as the data type of the (corresponding) parameter variable

  • If the data type of the value in x is different from the data type of the (corresponding) parameter variable:

    • The C compiler will insert code to convert the value before passing it to the function !

Example data type conversion in parameter passing

  • Example:

       float square(float x)
       {
          return x*x;
       }
    
       int main( )
       {
          int a, b;
          b = square(a); 
       }

  • The C compiler will insert the following conversion (casting) operations:

      square(a):       convert  2s compl code in a to IEEE 754
                       call  square
    
      b = ret-value;   convert  IEEE 754 code (ret-value) to 2s compl code
                       b = 2s compl code
    

Example data type conversion in parameter passing

  • In other words, the C compiler will insert these casting operations:

       float square(float x)
       {
          return x*x;
       }
    
       int main( )
       {
          int a, b;
          b = (int) square( (float) a); 
       }

  • Which results in:

      square(a):       convert  2s compl code in a to IEEE 754
                       call  square
    
      b = ret-value;   convert  IEEE 754 code (ret-value) to 2s compl code
                       b = 2s compl code
    

DEMO: demo/C/set1/param-conv.c + param-conv2.c (no conversion)