Declaration and definition
What you'll learn:
- call a function before its definition and tell who reports the error;
- choose whether a function gets a copy of the argument or the variable itself;
- overload functions and give arguments default values;
- write short functions as lambdas and capture variables in them.

Picture a machine that doubles numbers. Its front panel carries the name twice(int). Below it there is a slot where you drop a token with a whole number, and a tray where the result falls out. Inside sits the mechanism that does the work.
To use the machine, you do not need to see the mechanism: the panel tells you its name, what to put in and what comes out. A C++ function is built the same way:
- the panel is the function's first line: the return type, the name and the parameters;
- the mechanism is the body in curly braces.
The compiler reads a file from top to bottom. When it reaches a function call, it must already see the function's panel: its name, parameter types and return type. That is why a function defined below main cannot be called from main:
#include <iostream>
int main() {
std::cout << twice(21) << '\n'; // ✗
}
int twice(int x) {
return x * 2;
}error: use of undeclared identifier 'twice'twice has one parameter: the variable x in the function's declaration, which receives a value at the call. An argument is the value passed in the call: in twice(21) the argument is 21 and the parameter is x. On the machine, the parameter is the slot and the argument is the token you drop into it.
Moving twice above main makes the error go away. But if the function is defined in another file, there is nothing to move—the compiler must learn about it another way.
Declaration
A declaration tells the compiler a function's name, its parameter types and its return type. It is the machine's panel without the mechanism: a declaration has no body, and a semicolon takes its place.
int twice(int x);Parameter names in a declaration may be left out: int twice(int); means the same thing. The compiler needs only the types, while the name x tells the reader what goes into the function. A definition may leave the name out too, when the body does not use the parameter.
A definition is a declaration together with a body in curly braces, so a function defined above main needs no separate declaration. A function may be declared several times but defined only once.
Calling before the definition
A declaration above main solves the problem: the call compiles, and the body can stay at the end of the file.
#include <iostream>
int twice(int x);
int main() {
std::cout << twice(21) << '\n'; // 42
}
int twice(int x) {
return x * 2;
}To check the call twice(21), the compiler needs only the declaration: the function takes an int and returns an int. If the body is in another .cpp file, the linker connects the call to that body. Header files rely on this: they hold declarations, while definitions go in .cpp files. #include <iostream> works the same way: it inserts declarations from the standard library into the file.
Common mistake
The function is declared, but its definition is forgotten. The compiler sees no error, and the build stops at the next step:
int twice(int x);
int main() {
return twice(21); // ✗
}undefined reference to `twice(int)'The linker reports this, not the compiler. The declaration was enough to check the call, but no file of the program holds the body of twice. Here the machine analogy limps. A real panel with nothing behind it is plain to see, while a program with such a declaration compiles, and only the linker notices the gap. In the course exercises, the same message reads differently: undefined symbol: twice(int).
The same error appears when the definition's parameter types do not match the declaration. int twice(long x) is a different function, and the declared twice(int) is again left without a body.
Key points
- A parameter is a variable in the function's declaration; an argument is the value in the call.
- A declaration is the function's first line with a semicolon instead of a body; it is enough for the compiler to check a call.
- A definition is a declaration together with a body; a function may be declared many times but defined once.
- A declaration without a definition compiles, and the linker stops the build:
undefined referenceorundefined symbol.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.