Function Declaration and Prototypes in C

Function Declaration and Prototypes

Any C function by default returns an int value. More specifically, whenever a call is made to a function, the compiler assumes that this function would return a value of the type int. If we desire that a function should return a value other than an int, then it is necessary to explicitly mention so in the calling function as well as in the called function. Suppose we want to find out square of a number using a function. This is how this simple program would look like

main( ) 
{ 
 float a, b ; 
 printf ( "\nEnter any number " ) ; 
 scanf ( "%f", &a ) ; 
 b = square ( a ) ; 
 printf ( "\nSquare of %f is %f", a, b ) ; 
} 
square ( float x ) 
{ 
 float y ; 
 y = x * x ; 
 return ( y ) ; 
} 

And here are three sample runs of this program…

Enter any number 3
Square of 3 is 9.000000
Enter any number 1.5
Square of 1.5 is 2.000000
Enter any number 2.5
Square of 2.5 is 6.000000

Call by Value and Call by Reference

By now we are well familiar with how to call functions. But, if you observe carefully, whenever we called a function and passed something to it we have always passed the ‘values’ of variables to the called function. Such function calls are called ‘calls by value’. By this what we mean is, on calling a function we are passing values of variables to it. The examples of call by value are shown below

sum = calsum ( a, b, c ) ;
f = factr ( a ) ;

Leave a Comment