Sunday, October 30, 2011

Function Declaration and Prototypes in C Programming

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
The first of these answers is correct. But square of 1.5 is definitely not 2. Neither is 6 a square of 2.5. This happened because any C function, by default, always returns an integer value. Therefore, even though the function square( ) calculates the square of 1.5 as 2.25, the problem crops up when this 2.25 is to be returned to main( ). square( ) is not capable of returning a float value. How do we overcome this? The following program segment illustrates how to make square( ) capable of returning a float value.
 
main( )
{
float square ( float ) ;
float a, b ;
printf ( "\nEnter any number " ) ;
scanf ( "%f", &a ) ;
b = square ( a ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
}
float square ( float x )
{
float y ;
y = x * x ;
return ( y ) ;
}

And here is the output...
Enter any number 1.5
Square of 1.5 is 2.250000
Enter any number 2.5
Square of 2.5 is 6.250000
Now the expected answers i.e. 2.25 and 6.25 are obtained. Note that the function square( ) must be declared in main( ) as
float square ( float ) ;
This statement is often called the prototype declaration of the square( ) function. What it means is square( ) is a function that receives a float and returns a float. We have done the prototype declaration in main( ) because we have called it from main( ). There is a possibility that we may call square( ) from several other functions other than main( ). Does this mean that we would need prototype declaration of square( ) in all these functions. No, in such a case we would make only one declaration outside all the functions at the beginning of the program.
In practice you may seldom be required to return a value other than an int, but just in case you are required to, employ the above method. In some programming situations we want that a called function should not return any value. This is made possible by using the keyword void. This is illustrated in the following program.
 
main( )
{
void gospel( ) ;
gospel( ) ;
}
void gospel( )
{
printf ( "\nViruses are electronic bandits..." ) ;
printf ( "\nwho eat nuggets of information..." ) ;
printf ( "\nand chunks of bytes..." ) ;
printf ( "\nwhen you least expect..." ) ;
}
Here, the gospel( ) function has been defined to return void; means it would return nothing. Therefore, it would just flash the four messages about viruses and return the control back to the main( ) function.

No comments:

Post a Comment