< previous page page_65 next page >

Page 65
10905-0065a.gif
FIGURE 5.3
The header and body of a function.
return_type function_name ( [type [parameterName]]);
The function definition tells the compiler how the function works.
return_type function_name ( [type parameterName])
{
    statements;
}
A function prototype tells the compiler the return type, name, and parameter list. Functions are not required to have parameters, and if they do, the prototype is not required to list their names, only their types. A prototype always ends with a semicolon(;).
A function definition must agree with its prototype in return type and parameter list. It must provide names for all the parameters, and the body of the function definition must be surrounded by braces. All statements within the body of the function must be terminated with semicolons, but the function itself is not ended with a semicolon; it ends with a closing brace.
If the function returns a value, it should end with a return statement, although return statements can legally appear anywhere in the body of the function.
Every function has a return type. If one is not explicitly designated, the return type will be int. Be sure to give every function an explicit return type. If a function does not return a value, its return type will be void.
Here are some examples of function prototypes:

 
< previous page page_65 next page >

If you like this book, buy it!