Function Prototypes

Function prototyping is the major innovation of the ANSI standard for C. As we mentioned earlier, a function prototype gives the same information as the function's header: the name of the function, the type of value the function returns, and the number and type of parameters the function requires.

Summary: Function prototypes allow QuickC to check function references for accuracy.

Function prototypes normally appear near the start of the program, before the first function definition. Given the information in the prototype, QuickC can perform “type checking.” It checks each reference to the function—its definition, as well as every function call—to make sure that the reference uses the right number and type of arguments and the correct return value. Without type checking, it's easy to create bugs by passing the wrong type of value to a function or assuming the wrong return type.

C programs normally include one prototype for each function they define, except the main function. Most programmers don't bother to prototype main unless the program receives command-line arguments or returns a value to DOS when it ends. (For a discussion of command-line arguments, see topic .)

Here is the function prototype for the sphere function in VOLUME.C:

float sphere( int rad );

You can see that sphere expects a single int-type parameter and returns a value of type float. On the other hand, the prototype for showme in SHOWME.C indicates that showme expects three int-type parameters and returns nothing:

void showme( int a, int b, int c );

It's common to use the same parameter names in both the function prototype and the function header. In SHOWME.C, for instance, the showme function prototype,

void showme( int a, int b, int c );

uses the names a, b, and c, as does the header for that function,

void showme( int a, int b, int c )

Using the same names in both parameter lists makes the program more readable, but it's not a language requirement. The names in the prototype's parameter list are merely cosmetic. You can use any names you choose, or even omit the names completely. The prototype in SHOWME.C works just as well when written

void showme( int, int, int );

as when you supply the names a, b, and c. Both versions fully specify the number (three) and type (int) of the parameters the function expects.