Function parameters

Parameters can be passed to functions in C++ by value, by pointer and by reference.

Normal variables, i.e. no pointer variables, are passed to the function as function parameters by value. This means that during the execution of a function a copy of the variable is created on the stack of the processor.

In C++ there is also the possibility that the parameter passing is done by pointer. In this case the addresses of the variables, and not the variable itself, are passed to the function.

C++ has introduced another form of parameter passing, the passing by reference. This is identified by the fact that the function parameter is preceded by a &, which must not be confused with the address operator & in this context.

When using C++, parameter passing by reference is always preferable to passing by pointer.

Passing by Value

Code Explanation

This is a simple C++ program that defines a function add to perform addition of two integers. The function takes two arguments a and b, adds them, and returns the result.

In the main function, the add function is called twice, passing the arguments 1 and 3 and then 5 and 6. The returned result from the function calls is then printed to the console using the cout statement.

#include <iostream>
using namespace std;

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

int main() {
    cout << add(1, 3) << endl;
    cout << add(5, 6) << endl;
}

Output

4
11

Passing by Pointer

Code Explanation

This is a similar C++ program as the previous one, but the add function now takes two pointers a and b as arguments instead of two integers. The add function performs the addition of the values pointed to by the two pointers and returns the result.

In the main function, two variables a and b are declared and initialized with values 1 and 3, respectively. Another variable c is declared and initialized with value 5. The add function is then called twice, passing the addresses of a and b and then a and c. The returned result from the function calls is then printed to the console using the cout statement.

#include <iostream>
using namespace std;

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

int main() {
    int a = 1, b = 3, c = 5;
    cout << add(&a, &b) << endl;
    cout << add(&a, &c) << endl;
}

Output

4
6

Passing by Reference

This is another similar C++ program as the previous ones, but the add function now takes two references a and b as arguments instead of two integers or pointers. The add function performs the addition of the values referred to by the two references and returns the result.

In the main function, two variables a and b are declared and initialized with values 4 and 6, respectively. Another variable c is declared and initialized with value 12. The add function is then called twice, passing the references of a and b and then a and c. The returned result from the function calls is then printed to the console using the cout statement.

#include <iostream>
using namespace std;

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

int main() {
    int a = 4, b = 6, c = 12;
    cout << add(a, b) << endl;
    cout << add(a, c) << endl;
}

Output

10
16