The short answer
In C++, a function prototype is a declaration of a function’s name, return type and parameter types. It has no body and ends with a semicolon:
double area(double radius);
Why the word fits
A prototype is a first sketch of a function. It shows the shape of the function, meaning its name and the types it takes and returns, without the working code inside. Someone reading only the prototype knows how to call the function, but not what it does.
What the prototype tells the compiler
- Name: which function the code means.
- Return type: the type of value a call produces, so the compiler knows what the call’s result is.
- Parameter types: the type each argument must match or convert to.
- Parameter names: optional. They help readers but do not affect how calls are compiled.
Prototype next to definition
The two parts of a function are easy to compare when they sit side by side. This example uses a small square function:
int square(int x); // prototype: the shape only, ends with a semicolon
int square(int x) { // definition: the working code
return x * x;
}
The first line is the prototype. The braces and the return statement belong to the definition, which is covered in what is a function definition. The two must agree on the return type and parameter types.
Why the compiler cares
The compiler reads a file from top to bottom. A prototype lets you call a function earlier in the file than its definition. It also lets the compiler check each call against the prototype. A call with the wrong number of arguments does not compile. When an argument’s type can be converted to the parameter type, the compiler converts it, so an int passed to a double parameter becomes a double before the function runs.
Other names you may see
- Function declaration: the C++ standard’s term for this kind of statement. A prototype is a function declaration.
- Forward declaration: a declaration placed before the function is used or defined. A prototype written above
mainis a forward declaration.
For a function prototype, the two terms usually point to the same thing, so you can read them as equivalent in course notes and documentation.
Where you will meet the word
- In textbooks and course notes, usually in the first few lessons on functions.
- In header files, where prototypes often sit in a
.hfile and the function body lives in a.cppfile. - In code reviews, when a function is used before its definition appears in the file.
If declarations in general are new to you, look at what is declaration in c. For the full picture with syntax, examples and common mistakes, see what is a prototype in c.
Summary
- In C++, a function prototype declares a function’s name, return type and parameter types.
- It has no body, and it ends with a semicolon.
- The definition supplies the body, and the two must agree.
- Prototypes let you call a function before its definition and let the compiler check each call.