Call by value and call by reference are two different techniques to call a function or a method in a program. The call-by-value technique only passes the values of the variables. On the other hand, call by reference technique passes addresses of the variables.
If you are passing arguments to a function using the ‘call by value’ technique, then changes to the variables inside the function won’t affect the original values of those variables.
If you pass arguments to a function using the ‘call by reference’ technique, then changes to variables inside the function would affect the original values of those variables.
In this section, we will discuss these techniques in detail. We will also try to identify the differences between these two techniques.
Content: Call By Value Vs Call By Reference
Comparison Chart
Basis for Comparison | Call By Value | Call By Reference |
---|---|---|
Basic | A copy of the variable is passed. | A variable itself is passed. |
Effect | Change in a copy of variable doesn't modify the original value of variable out side the function. | Change in the variable affect the value of variable outside the function also. |
Calling Parameters | function_name (variable_name1, variable_name2, . . . .); | function_name (&variable_name1, &variable_name2, . . . .); //in case of object object.func_name( object); |
Receiving Parameters | type function_name (type variable_name1, type variable_name2, . . . .) { . . } | type function_name ( type *variable_name1, type *variable_name2, . . . .) { . . } //in case of object type function_name(class_type object_name) { . . } |
Default calling | Primitive type are passed using "call by value". | Objects are implicitly passed using "call by reference". |
Memory Location | Actual and formal arguments are created in different memory location | Actual and formal argument are created in same memory location |
Secure | Actual arguments are more secure as they cannot be modified accidently | Actual arguments are less secure as they can be modified accidentally |
What is Call By Value?
Call by value is one parameter passing method where the actual parameter values are copied to the formal parameters. These actual and formal parameters are stored at different memory locations.
Thus when the function anyhow modifies the values of formal parameters, the values of actual parameters are not affected.
Advantages of Call by Value
- The call-by-value technique does not change the original value of the passed variable.
- It avoids accidental changing of values of actual variables.
Example of Call by Value
Let’s see an example to understand this briefly.
//example in Java class check { void change( int i, int j) { i = i*i; j = j/2; System.out.println( "value of parameter inside the function"); System.out.println( " value of 'i' which accepts the value of argument 'a' " +i); System.out.println( " value of 'j' which accepts the value of argument 'b' " +j); } } public class call_by_value { public static void main(String args[]) { int a=12, b=20; check C= new check(); System.out.println("value of 'a' and 'b' before function call - " + a +" " +b); C.change(a,b); //call by value. System.out.println("value of 'a' and 'b' after function call - " + a +" " +b); } } //output value of 'a' and 'b' before function call - 12 20 value of parameter inside the function value of 'i' which accepts the value of argument 'a' 144 value of 'j' which accepts the value of argument 'b' 10 value of 'a' and 'b' after function call - 12 20
What is Call By Reference?
Call by Reference method passes a reference/address of variables to the function as an actual parameter. Here, the actual and formal parameter shares the same memory location.
During function calls, the addresses of variables in actual parameters are copied to pointer variables of formal parameters. The function’s formal parameter refers to the variable’s original value. So any changes made to the formal parameters affect the variable’s original value.
In C++ and Java, it is very common to pass the object to the function/method and objects are always passed by its reference. Thus, changes done to the object inside the function/method affect the object used to invoke that function/method.
Therefore, the function deals with original data, and the changes made to the original data reflect back in the referenced data.
Reference parameter characteristics
- A reference variable can never have a null value. Hence it always refers to a legit object (variable).
- Once assigned, we can not make it target a different object.
- There is no need to implement an explicit mechanism to a reference variable in order to dereference the memory address and retrieve the actual data value.
Example of Call by Reference
The following fragment shows the correct way to ‘call by reference’.
//example in C++ #include <iostream> using namespace std; void swap( int *x, int *y) { int temp; temp=*x; *x=*y; *y=temp; } int main() { int a=10, b=20; cout<<"value of a, b before the function call - "<<a<<" "<<b<<'\n'; swap(&a, &b); //call by reference. cout<<"value of a, b after the function call - "<<a<<" "<<b; return 0; } //output value of a, b before the function call - 10 20 value of a, b after the function call - 20 10
Now let’s discuss ‘call by reference’ by passing an ‘object’ as an argument, which is implicitly passed by the approach ‘call by reference’.
class check { int a, b; check( int x, int y) { // object initialized through this constrtuctor a=x; b=y; } void exchange(check ob) { ob.a= ob.a*2; ob.b= ob.b/2; } } class Main_class { public static void main(String args[ ]) { check C = new check(20,40); //object initialization. System.out.println("value of 'ob.a' and 'ob.b' before function call - " + C.a +" " + C.b); C.exchange(C); //call by reference. System.out.println("value of 'ob.a' and 'ob.b' before function call - " + C.a +" " + C.b); } } //output value of 'C.a' and 'C.b' before function call - 20 40 value of 'C.a' and 'C.b' before function call - 40 20
Key Differences Between Call By Value and Call By Reference
- Passing the argument by using the ‘call by value’ approach only passes the copy of that variable, so changes made to the value in the copy of that variable do not affect the original value of that variable. On the contrary, in ‘call by reference’ approach, the variable itself is passed as an argument, so changes to it modify the value of the original variable.
- If the arguments passed are primitive datatypes they are simply ‘call by value’. As against, if the references/addresses of the arguments or objects are passed, then a function is called by ‘call by reference’ method.
- In ‘call by value approach’, the arguments passed are only the name of variables. Conversely, in the ‘call by reference’ approach, the arguments passed are variable names along the ‘&’ sign, or an object passed just by its name.
- Receiving parameters of the argument in ‘call by value’ approach are variable names along with their data type. On the other hand, in ‘call by reference’ approach, the receiving parameter is always a pointer variable along with the data type, and in the case of the object, it is an object name along with its class type.
- Call by value method creates a different memory location for the actual and formal parameters. Conversely, call by reference method has the same memory location for both actual and formal parameters.
- With the ‘call by values’ method, the original values of the variable are more secure as compared to the ‘call by reference’ method.
Conclusion
C++ and Java use both approaches depending on what is passed. If you want to pass only the variable’s value, use the ‘call by value’ approach, and if you want to see the change in the variable’s original value, then use the ‘call by reference’ approach.
Avinal Kumar says
I read few comparisons and wow they are amazingly explained. Nice work.
Aiswaryaa says
Amazing explanation