Skip to main content

13.7) Function Prototypes


The first part of Exercise 20 showed you that a program fails to compile if a function call is seen by the compiler before it has seen the definition of that function.

This can be a problem, as we can't always arrange things so that a function definition is seen before use; for example, consider a situation in which we wish to split a program across multiple files, with function definitions in one file and the program that uses them in another. (This is fairly common and is explored in detail later).

The second part of Exercise 20 demonstrates the solution: a function prototype that declares the function's name, its return type and the types of its parameters. This information is all the compiler needs in order to generate code that calls the function.

The code of the function body still needs to be provided, but as long as this is seen no later than the linking stage, your program will compile to an executable successfully.