Page 74 - 6437
P. 74
Function Body: The function body contains a collection of statements that
define what the function does.
Example
Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two:
Function Declarations
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
A function declaration has the following parts:
return_type function_name( parameter list );
For the above defined function max(),the function declaration is as follows:
int max(int num1, int num2);
Parameter names are not important in function declaration, only their type is required, so
the following is also a valid declaration:
int max(int, int);
Function declaration is required when you define a function in one source file and you
call that function in another file. In such case, you should declare the function at the top of the file
calling the function.
77