Definition and declaration

The definition of a function specifies exactly how a function is constructed, what parameters are passed to it, and what value it returns to the calling function.

With the declaration we name a variable and make it known to the compiler.

Definition and declaration differ in some points. The definition includes a memory allocation for the function, while no memory is allocated in the declaration. The declaration can be done several times, vice versa a function can be defined exactly once in a program.

Parameter vs. Argument

The terms parameter and argument are often used synonymously. Parameters are variables that are declared in the function signature. Arguments are values that are used when the function is called. Parameters are declared in round brackets of the function signature. The parameters generally dictate how a function must be called.

In programming languages, a distinction is made between two main forms of parameter passing: call-by-value and call-by-reference. Parameters can be passed to functions in C++ by value, by pointer or by reference.

Syntax

return_type function_name(type parameter_1, type parameter_2, ...) {
	// code to be executed
}

Example

Code Explanation

LineDescription
4Defines the function add() with the input parameters a and b of type integer.
The return value of the function is of type integer.
5Returns the sum of variable a and b
8defines the main function which expects a return value of type integer
9calls the add() function with the values 5 and 7 in an output stream
11The main() function expects a return value of type integer according to the declaration. Therefore the value 0 is returned
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << add(5, 7);

    return 0;
}
Output
12