What a function prototype is
A function prototype is a declaration that tells the compiler a function’s name, return type and parameter types before the function is defined or called. It ends with a semicolon and has no body:
int add(int a, int b);
Read it from left to right: add is a function that takes two int values and returns an int. With that line in place, the compiler can check every call to add, even when the code that does the adding appears further down the file.
The prototype is one half of the picture. The other half is the definition, which provides the body that runs:
int add(int a, int b) {
return a + b;
}
For the body side on its own, see what is a function definition. A function can be declared many times, but an ordinary function can be defined only once in a program.
Why C++ needs prototypes
The compiler reads a source file from top to bottom, and it must know a name before the code that uses that name. If you call a function that has not been declared yet, the program does not compile. A prototype gives the compiler that knowledge early.
A prototype also lets the compiler check each call. It counts the arguments, compares their types with the parameter types, and converts them where C++ allows. Take this declaration:
double half(double x);
A call such as half(5) passes the integer 5, which is converted to the double value 5.0 before the function runs. A call with the wrong number of arguments, such as add(1) for the two-parameter add above, does not compile.
A complete example
This program declares add above main, calls it from main, and defines it below:
#include <iostream>
int add(int a, int b); // prototype
int main() {
std::cout << add(2, 3) << '\n'; // prints 5
return 0;
}
int add(int a, int b) { // definition
return a + b;
}
Output:
5
The call in main works because the prototype appears first. The definition can come later without causing a problem.
What goes wrong without one
Remove the prototype and move add below main:
#include <iostream>
int main() {
std::cout << add(2, 3) << '\n'; // error: 'add' was not declared
return 0;
}
int add(int a, int b) {
return a + b;
}
The compiler stops with an error saying that add was not declared. The exact wording depends on your compiler. You can fix the problem in one of two ways: put the prototype above main, or move the whole definition above main.
Parameter names and empty parentheses
Parameter names in a prototype are optional. These two lines mean the same thing to the compiler:
int add(int a, int b);
int add(int, int);
Names help people read the code. They do not have to match the names in the definition, although matching them is easier to follow.
In C++, empty parentheses mean that a function takes no parameters:
void print_banner();
The form void print_banner(void); also works in C++, but the empty parentheses are the more usual style.
Default arguments belong in the prototype
A prototype can give a parameter a default value. C++ does not allow the default to be repeated in the definition, so it goes in the prototype:
#include <iostream>
#include <string>
void greet(std::string name = "friend"); // default set in the prototype
int main() {
greet(); // prints Hello, friend
greet("Ada"); // prints Hello, Ada
return 0;
}
void greet(std::string name) { // no default here
std::cout << "Hello, " << name << '\n';
}
Output:
Hello, friend
Hello, Ada
Prototypes in header files
Larger programs are split across several files. Prototypes usually go in a header file, and each source file that calls the function includes that header. The definition goes in one source file.
math_utils.h:
#ifndef MATH_UTILS_H
#define MATH_UTILS_H
int add(int a, int b);
#endif
math_utils.cpp:
#include "math_utils.h"
int add(int a, int b) {
return a + b;
}
main.cpp:
#include <iostream>
#include "math_utils.h"
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
Build and run it with:
g++ main.cpp math_utils.cpp -o app
./app
The output is 5.
The #ifndef, #define and #endif lines make up a header guard. They stop the header’s contents from being read twice in the same file, which would cause a redefinition error. Some compilers also accept #pragma once for the same job. If you have seen a header guard in c code before, this is the same pattern in C++.
The definition stays out of the header. If it were placed there, every source file that includes the header would define add again, and the linker would report a multiple-definition error.
Common mistakes
- Mismatched return types. A prototype
int add(int a, int b);followed bydouble add(int a, int b) { ... }is an error. The return types must match. - Declared but never defined. A prototype with no definition compiles as long as nothing calls the function. Once something calls it, the linker reports an undefined reference.
- Different parameter types.
int add(int, int);andint add(long, int)are two different functions, because their parameter types differ. A call toadd(2, 3)then looks for theintversion, which has no definition, so the link fails. Two functions with one name and different parameter types are an overload, and what is function overloading explains the rules. - Missing semicolon. A prototype needs a semicolon at the end. Without it, the compiler reports a syntax error.
- Definition in a header. Covered in the header section above.
Summary
- A function prototype declares a function’s name, return type and parameter types, and it ends with a semicolon.
- It lets the compiler check each call, and it lets you call a function before its definition appears in the file.
- Parameter names are optional, and default arguments go in the prototype.
- In larger programs, prototypes usually go in header files and definitions in source files.