Updated on 08 May 2020 | 3 Min Read
By
 

Looking at most of the programming languages including C++, the functions can be declared in two main ways:

  • Call by Value
  • Call by Reference.

So, in this blog, we shall discuss what are these two types of methods to invoke functions and also the difference between call by value and call by reference. So, let’s get on with it!

What does a Call by Value method mean?

A Call by value method basically copies the data/value in any argument to a formal parameter within the particular function. Hence, any updates or changes made to the main function’s parameter do not affect the overall argument in any way.

So, in this method, the values of parameters are copied to the original function’s formal parameters and these are stored in various memory locations individually. Therefore, any changes made within the function are not reflected in the actual parameter of the caller.

Example of a Call by Value method

void main()
{
int x = 10,
void increment(int);
Cout << "before calling function" << x;
increment(x);
Cout << "after calling function" << x;
getch();
void increment(int a) {
int a = a + 1;
Cout << "the value is" << a;
}

Output:

before calling function10
the value is 11
after calling function 1-0

Now that we have gone through what a Call by Value method is, let’s hop on to the next segment, i.e. Call by Reference.

So, what is meant by a Call by Reference method?

The Call by reference method actually copies the address of an argument within a function to the formal parameter. Here, you need to understand that the address used to access the formal(actual) argument is used in the function call. To make things simpler, the changes made in the original parameter are altered onto the passing argument.

In a Call by Reference method, memory allocation is the same as to the formal(actual) parameters. Meanwhile, all the operations being performed within the function are based on the value stored at the address of the actual parameter and the modified value shall be stored in the same address.

Public static void(string args[]) {
int b = 10;
System.out.println("Before call Value of b = ", b);
Void increment();
System.out.println("After call Value of b = ", b);
}
Void increment(int y) {
int y = y + 1;
}

Output

Before call Value of b =10
After call Value of b =11

Now that we have cleared our concepts about these two methods for most programming languages let’s get a hold on the difference between call by value and call by reference in c++.

Call by Value Call by Reference
In this, you pass values by copying variables onto passing arguments. In this, instead of copying the values of variables, the address is copied.
The copy of the variable is passed. The variable itself is passed.
Changes made to the copied variable do not modify values outside the function. Changes made in the copied variable do modify the values outside the function.
The values of the variables are passed on easily. Pointer variables are needed to store the address of actual variables.
Formal and actual arguments are not stored in the same location. Stored in the same location.
Actual arguments remain safe as original values are not modified. Actual arguments are not as safe as values are modified.