The short answer
A function definition is the complete code for a C++ function. It gives the function a return type, a name, a parameter list, and a body in curly braces. The body holds the statements that run when the function is called:
int add(int a, int b) {
return a + b;
}
The parts of a definition
- Return type: the type of value the function sends back to the caller. Here it is
int. A function that returns nothing usesvoid. - Name: the identifier the rest of the program uses to call the function. Here it is
add. - Parameter list: the inputs, in parentheses. Each parameter has a type and a name, so
(int a, int b)takes twointvalues. A function with no inputs has empty parentheses. - Body: the statements inside braces. Here the body is
return a + b;, which sends the sum back to the caller.
A function with a non-void return type should reach a return statement. If control reaches the end of such a function without returning a value, the behavior is undefined. The exception is main, which returns 0 automatically if you leave out the return.
If functions are new to you, start with what is a function in c.
How a definition differs from a prototype
A prototype has the same first line as a definition, but it ends in a semicolon and has no body:
int add(int a, int b); // prototype: a declaration with no body
int add(int a, int b) { // definition: includes the body
return a + b;
}
The return type, the name and the parameter types must match between the two. The parameter names can differ, although most people keep them the same.
The first line is what most tutorials call a prototype. If that word is unfamiliar, what is meant by prototype explains it. Readers who want the terms compared side by side can look at what is a function declaration vs definition.
Where the definition goes
A definition can sit before or after the code that calls it. If it comes after the call, put a prototype above the call. The rules for that pairing are covered in what is a prototype in c. If the definition comes first, it also works as a declaration, so no separate prototype is needed:
#include <iostream>
int add(int a, int b) { // definition first, so it also declares add
return a + b;
}
int main() {
std::cout << add(2, 3) << '\n';
return 0;
}
Output:
5
Only one definition per function
An ordinary function can be defined only once in a program. Two definitions with the same name and parameter types cause an error. If both are in the same file, the compiler reports a redefinition. If they are in different files, the linker reports a multiple-definition error.
A function that returns nothing
A void function has no return value, so its body does not need a return statement:
#include <iostream>
void say_hello() {
std::cout << "Hello!\n";
}
int main() {
say_hello();
return 0;
}
Output:
Hello!
Common mistakes
- Leaving out the return type. C++ requires one on a function definition. Constructors are the exception, because they have no return type.
- Prototype and definition that disagree. The return type and parameter types must match.
- Defining the same function twice. Keep one definition per function, and put the prototype in a header if several files need it.
- Forgetting to return a value. A function that should return an
intbut reaches its closing brace without areturnhas undefined behavior.
Summary
A function definition is the complete function: the return type, the name, the parameter list, and the body in braces. A prototype gives only the first line and ends with a semicolon. Keep the two in agreement, and define each ordinary function once.