Definition of Function Prototypes in C and C++

Function prototypes save debugging time in C and C++

A function prototype is a declaration in C and C++ of a function, its name, parameters and return type before its actual declaration. This enables the compiler to perform more robust type checking. Because the function prototype tells the compiler what to expect, the compiler is better able to flag any functions that don't contain the expected information. A function prototype omits the function body.

Unlike a full function definition, the prototype terminates in a semi-colon. For example:

intgetsum(float * value) ;

Prototypes are most often used in header files—although they could appear anywhere in a program. This allows external functions in other files to be called and the compiler to check the parameters during compilation.

Purposes of a Function Prototype

  • A function prototype ensures that calls to a function are made with the correct number and types of arguments.  

  • A function prototype specifies the number of arguments.

  • It states the data type of each of the passed arguments.

  • It gives the order in which the arguments are passed to the function.

The function prototype tells the compiler what to expect, what to give to the function and what to expect from the function.

Benefits of Function Prototypes

  • Prototypes save debugging time.
  • Prototypes prevent problems that occur when you compile using functions that were not declared.
  • When function overloading occurs, the prototypes distinguish which function version to call.