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.
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.
return_type function_name(parameter_list) {
// Function body
}
int
, float
, void
).{}
, containing statements that define what the function does.#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
cout << add(5, 7);
return 0;
}
12
add
int add(int a, int b) {
add
that takes two integer parameters a
and b
and returns their sum as an integer.return a + b;
a
and b
and returns the result.add
Functioncout << add(5, 7);
add
function is called with arguments 5
and 7
. The returned sum (which is 12
) is then printed to the console using cout
.return 0;
main
function.