Last Updated:

C++ Function Prototypes

So far, the sample programs here have used function prototypes without explanation. Now it's time to talk about them in detail. In C++, all functions must be declared before they can be used. This is usually implemented using a function prototype. Prototypes contain three kinds of information about a function:

■ the type of value it returns;
■ the type of its parameters;
■ Number of parameters.


Prototypes allow the compiler to perform the following three important operations:

■ They tell the compiler what type of code to generate when the function is called. Differences in the types of parameters and the value returned by the function allow for different processing by the compiler.
■ They allow C++ to detect and report invalid conversions of argument types used when calling a function to the type specified in its parameter declaration.
■ They allow the compiler to identify differences between the number of arguments used when calling a function and the number of parameters specified in the function definition.

The general form of a function prototype is similar to its definition, except that the function body is not represented in the prototype.


type func_name (type parm_name1, type parm_name2,


type parm_nameN);

Using parameter names in the prototype is optional, but allows the compiler to identify any type mismatch when an error occurs, so it is better to include parameter names in the function prototype.

To better understand the usefulness of function prototypes, consider the following program. If you try to compile it, you will receive an error message from the compiler because this program attempts to call the sqr_it function () with an integer argument, and not with a pointer to an integer value (according to the prototype of the function). The error is that the conversion of an integer value to a pointer is not valid.


/* This program uses a function prototype that allows for strict type control.


void sqr_it (int *i); prototype function

int main ()


int x;

x = 10;

sqr_it (x); *** Error *** - Type Mismatch!

return 0;


void sqr_it (int *i)


*i=*i * *i;


Importantly! Although the C language allows prototypes, their use is not mandatory. The fact is that in the first versions of C they were not used. Therefore, when translating old C-code into C++ code, before compiling the program, it is necessary to ensure the availability of prototypes for absolutely all functions.