Information needed to translate a function call
 

  • Just like the case of global variables:

      • The C compiler needs data type information in the function definition (data type of the parameters and the return value) in order to translate a function call

  • Programming rule in C due to a 1-pass C-compiler:

      • A function must be defined or declared before it is used   

        (Or else, compile error or runtime error may occur)

Example of correct usage of function in C
 

Function f( ) is defined before it was used:

#include <stdio.h>

float f(float a, float b)  // Compiler knows params are floats
{
   return(a*a + b*b);
}

int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;

   z = f(x,y);  // Compiler will convert int to float 
   printf("%d^2 + %d^2 = %d\n", x, y, z);
}    

DEMO:   /home/cs255001/demo/tmp/demo.c

Example of incorrect usage of function in C
 

Function f( ) is not defined or declared before it was used:

#include <stdio.h>

int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;

   z = f(x,y);  // Compiler will pass x,y as int 
   printf("%d^2 + %d^2 = %d\n", x, y, z);
}

float f(float a, float b)  // Function needs float - error ! 
{
   return(a*a + b*b);
}    

In this case, the compiler can detect the error and reports an error

Declaring a function
 

  • Declaring a function = tell the C compiler the data types of the parameters and the return value of a function

      • The C compiler need these information to perform the correct translation to assembler code !!!

  • Syntax to declare a function:

       [extern] returnType funcName( parameters ) ;  
      

    The keyword extern in a function declaration is optional (because even without the keyword extern, the function declaration is distinguishable from a function definition

Example of correct usage of function using a declaration
 

Function f( ) is now declared before it was used:

#include <stdio.h>

float f(float a, float b);  // Function declaration

int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;

   z = f(x,y);  // Using function f( ) 
   printf("%d^2 + %d^2 = %d\n", x, y, z);
}

float f(float a, float b)  // Function definition  
{
   return(a*a + b*b);
}    

DEMO:   /home/cs255001/demo/tmp/demo.c

Important usage of function declaration: declaring functions defined in another program file

Function declarations are needed when the functions are defined inside a different file:

progFile1.c progFile2.c
#include <stdio.h>



int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;

   z = f(x,y);  // Using function f( ) 
   printf("%d^2+%d^2=%d\n", x, y, z);
}   
// Function f( ) is defined 
// in this file

float f(float a, float b) 
{
   return(a*a + b*b);
}



   

In this case, the C compiler cannot detect the error !!

You see the error whan you run the program, it outputs 3^2 + 4^2 = 0 !!??
(Compiler had generated incorrect assembler instructions !)

Important usage of function declaration: declaring functions defined in another program file

Function declarations are needed when the functions are defined inside a different file:

progFile1.c progFile2.c
#include <stdio.h>

float f(float a, float b); 

int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;

   z = f(x,y);  // Using function f( ) 
   printf("%d^2+%d^2=%d\n", x, y, z);
}   
// Function f( ) is defined 
// in this file

float f(float a, float b) 
{
   return(a*a + b*b);
}



   

The function declaration will fix the error

DEMO:   /home/cs255001/demo/tmp/demo.c + demo2.c

Postcripts on declarations of functions

You can have multiple (non-conflicting) declarations of the same function:

progFile1.c progFile2.c
#include <stdio.h>

float f(float a, float b); 
float f(float a, float b); 
int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;

   z = f(x,y);  // Using function f( ) 
   printf("%d^2+%d^2=%d\n", x, y, z);
}   
// Function f( ) is defined 
// in this file

float f(float a, float b) 
{
   return(a*a + b*b);
}



   

You will get compile errors if the declarations are conflicting (not the same).

Postcripts on declarations of functions

You can put function declarations inside a function:

progFile1.c progFile2.c
#include <stdio.h>


int main(int argc, char *argv[])
{
   int x = 3, y = 4;
   int z;
   float f(float a, float b); 

   z = f(x,y);  // Using function f( ) 
   printf("%d^2+%d^2=%d\n", x, y, z);
}   
// Function f( ) is defined 
// in this file

float f(float a, float b) 
{
   return(a*a + b*b);
}



   

In this case, the function declarations are subjected to the scoping rules for local variables !