Definition of Function Prototypes in C and C++

Hacker teams brainstorming working hackathon at laptops in workshop

Hero Images/Getty Images

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

  • 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

  • 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.
Format
mla apa chicago
Your Citation
Bolton, David. "Definition of Function Prototypes in C and C++." ThoughtCo, Aug. 27, 2020, thoughtco.com/definition-of-function-prototypes-958077. Bolton, David. (2020, August 27). Definition of Function Prototypes in C and C++. Retrieved from https://www.thoughtco.com/definition-of-function-prototypes-958077 Bolton, David. "Definition of Function Prototypes in C and C++." ThoughtCo. https://www.thoughtco.com/definition-of-function-prototypes-958077 (accessed March 28, 2024).