Function part 5 (Call By Reference vs Call By Value)

Estimated reading: 3 minutes 184 Views

Call by value and call by reference are two different ways to pass arguments to functions in C++.

Call by value is the default passing mechanism in C++. When a function is called by value, a copy of the actual argument is passed to the function. The function then operates on this copy, and any changes made to the copy inside the function will not be reflected in the actual argument outside the function.

Call by reference allows you to pass the reference of the actual argument to the function. This means that the function operates directly on the actual argument outside the function. Any changes made to the argument inside the function will be reflected in the actual argument outside the function.

To pass an argument by reference, you need to use the & operator before the argument name

For example, the following code shows how to call a function by value:

#include <iostream>
using namespace std;

void swap(int x, int y)
{
    int z = x;
        x = y;
        y = z;
}

int main()
{
    int x = 10, y = 20;
    swap(x, y);
    cout << "x= " << x << " y= " << y << endl;
    
    return 0;
}

//x= 10 y= 20

The following code shows how to call the same function by reference:

#include <iostream>
using namespace std;

void swap(int&x, int&y)
{
    int z = x;
        x = y;
        y = z;
}

int main()
{
    int x = 10, y = 20;
    swap(x, y);
    cout << "x= " << x << " y= " << y << endl;
    
    return 0;
}

//x= 20 y= 10

As you can see, the result of calling the swap() function is different depending on whether it is called by value or by reference.

Call by reference is often used to modify the values of the arguments outside the function. For example, the swap() function could be used to swap the values of two variables.

Call by reference can also be used to pass large objects to functions without having to copy them. This can improve the performance of your program.

However, it is important to use call by reference carefully, as it can lead to unexpected results if not used correctly. For example, if you pass a reference to a local variable to a function, and the function returns, the local variable will go out of scope, but the reference to it will still be valid. This can lead to a program crash if you try to access the local variable through the reference.

In general, it is best to use call by value unless you have a specific reason to use call by reference.

For example, the following code shows how to call a function by value:

#include <iostream>
using namespace std;

void fun(int x, int y)
{
    x += 1;
    y += 2;
}
int main()
{
    int k = 50, r = 10;
    fun(k, r);
    cout << "K= " << k << " R= " << r << endl;
    
    return 0;
}

//K= 50 R= 10

The following code shows how to call the same function by reference:

#include <iostream>
using namespace std;

void fun(int&x, int&y)
{
    x += 1;
    y += 2;
}
int main()
{
    int k = 50, r = 10;
    fun(k, r);
    cout << "K= " << k << " R= " << r << endl;
    
    return 0;
}

//K= 51 R= 12

As you can see, the result of calling the fun() function is different depending on whether it is called by value or by reference.

Share this

Function part 5 (Call By Reference vs Call By Value)

Or copy link

CONTENTS
English