Difference Between Call by Value and Call by Reference
In languages such as C++ or Java, either 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:
Key Differences Between Call by Value and Call by Reference
Call by Value | Call by Reference |
Copying variables pass values. | 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 changed | The 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. |
What is Call by Value?
Call by value is known as the method for parameter passing. In this method of calling by value, the actual parameter's value is copied in the formal parameters. 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.
This method makes it impossible to change or modify the actual parameter's value by using a formal 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 with an Example
In call by value, a copy of the argument's value is passed to the function, and any modifications made to the parameter inside the function are not reflected outside the function.
#include <stdio.h>
void increment(int x) {
x = x + 1;
}
int main() {
int a = 5;
increment(a);
printf("The value of a is %d\n", a); // Output: The value of a is 5
return 0;
}
In call by reference, a reference to the argument's memory location is passed to the function, so any modifications made to the parameter inside the function are reflected outside the function.
#include <stdio.h>
void increment(int *x) {
*x = *x + 1;
}
int main() {
int a = 5;
increment(&a);
printf("The value of a is %d\n", a); // Output: The value of a is 6
return 0;
}
In Example 1, we saw that any modifications made to the parameter inside the function were not reflected outside the function when using call by value. In Example 2, we saw that modifications made to the parameter inside the function were reflected outside the function when using call by reference.
Choosing Between Call by Value and Call by Reference
The choice between call by value and call by reference depends on the specific requirements and constraints of your program, and understanding the difference between the two is important for making the right decision.
Call by value is generally preferred when you want to prevent any unintended changes to the original data. This can be useful when you want to keep the original data intact, or if you don't want the function to affect any other parts of the program. It is also typically used for small data types, such as integers or characters because it is less memory-intensive.
Call by reference, on the other hand, is often used when you want to modify the original data in the function or if you're working with large data types, such as arrays or structures. It is generally more efficient than call by value because it avoids creating a copy of the data, which can be especially important when working with large amounts of data.
Comments
write a comment