Difference Between Call by Value and Call by Reference

By Mohit Uniyal|Updated : April 28th, 2022

Difference Between Call by Value and Call by Reference: To invoke any function in any programming language, it can be done in two ways which are Call by value and call by reference. The major difference between call by value and call by reference is that in the call by value, a copy of the value is passed whereas in the call by reference address of the value is passed. 

Here we will first explore what is called by value and call by reference in brief thereafter we will see the difference between the two methods. Here we have discussed the difference between call by value and call by reference-based on various factors which we will see in the coming sections.

Table of Content

What is Call by Value?

Call by value is known as the method for parameter passing. In this method of calling by value, in the formal parameters, the value of the actual parameter is copied. In this, a different memory is allocated for each type of parameter. The advantage of using the call by value method is that the original variable does not change with the change in calling by value.  

In this method by using a formal parameter, it is not possible to change or modify the value of the actual parameter. The actual value of the actual argument is not varied either. Let us see an example of a Call by value"

Input: 

void main() {
int a = 5,
void decrement(int);
Cout << "before function calling" << a;
increment(a);
Cout << "after function calling" << a;
getch();

void increment(int x) {
int x = x - 1;
Cout << "value is" << x;
}

Output:

before function calling 5
value is 5
after function calling 4

What is Call by Reference?

Call by reference is also a method for parameter passing. In this method address or reference is passed to a function. The call by reference parameter is also known as the pointer variable. The original value of the function changes with a call by reference. In languages such as C++ or Java, call by reference is preferred to call by value.

Let us see the example of call by reference before finally reading about the difference between call by value and call by reference.

Input:

void swap(int *c, int *d){

int temp;

temp=*c;

*c=*d;

*d=temp;

}

void main(){

int c=200, d=400;

swap(&a, &b); // passing value to function

printf("nValue of a: %d",a);

printf("nValue of b: %d",b);

}

Output:

Value of a: 400
Value of b: 200

Difference Between Call by Value and Call by Reference

In languages such as C++ or Java, either of the following call by reference or call by value approaches can be used based on the requirement of the programmer. Let us now see the difference between call by value and call by reference in the following table in order to know where to use which method for better results:

Difference Between Call by Value and Call by Reference

Call by Value

Call by Reference

Values are passed by copying variables.Values are passed by copying the address of the variables.
Copies are passed.Variable is passed.
Changes do not reflect the original function.Changes affect the variable of the function.
The actual variable can not be changedThe actual variable can be modified.
The argument is also safe from the changes.The argument also changes with a change in the called function.
The different memory location is used to create the actual and formal arguments.The same memory location will be used.
Used in C++, PHP, C#, etc.Used in Java, C++, etc.

Comments

write a comment

FAQs

  • The major difference between Call by Value and Call by Reference is that in the call by value, changes done on the called variable will not reflect on the original variable whereas changes are done in the called variable called by reference will change the original variable of the function.

  • In the call by value method, the variable is called using its value. The value of the original argument can not be changed by using this method. A different memory location is allotted in this method.

  • The call by reference method is used to call the variable by using its reference or address. The variable is also known as pointer variable. The same memory is used for the calling.

  • As per the susceptible changes in argument, the difference between call by value and call by reference is that in all by value, the argument is safe as no changes are implemented on them if the change is done to the called variable whereas for the called by reference function, changes will be seen in the argument of changes are done to the called variable.

Follow us for latest updates