In C++ and Java, there are two ways to call a function or a method. The first is “call by value” and the second is “call by reference”. The main difference between both the methods is, call by value method passes the value of a variable and call by reference passes the address of that variable.
Call by value method passes only the value of a variable to the function code. If there is any change in the value of a variable inside that function, it does not affect the original value of that variable. In call by reference method, we pass the variable itself in an argument, and the change in the value of a variable also affects the original value of that variable.
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". |
Definition of Call By Value
If you pass a primitive data type (integer, character, and string ) to a function/method, then only it’s “value” is passed to the function code. The function copies that value of an argument to a ‘formal parameter’ of the function code. If there is any modification to the formal parameter in a function code, it will not modify the original value of the argument which is used to call that function.
In simple words, if a function/method is called by ‘call by value’ approach; then a copy of the variable is passed to the function code. If a function code makes any changes to the value in the copy of the variable, it doesn’t change the original value of the variable.
Points to recall
The primary benefit of employing the call by value method is that the data passed through variables does not change the actual data. The reason behind this is that the changes applied to the variables inside the functions reflects only to the function copy of the argument value and the original value of the argument remains unchanged.
Example
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
Definition of Call By Reference
Call by Reference method passes a reference/address of an argument to the function code. As the address of an argument is passed to the function code, the formal parameter accepting that address would be a ‘pointer’ variable. Now, as function code has obtained the address of an argument, the modification in the value of an argument will also modify the original value of an argument.
In C++ and Java, it is very common to pass the object to the function/method and object are always passed by its reference. Changes done to the object inside the function/method affects the object used to invoke that function/method.
Points to Recall
The reference method uses reference variables which permits us to pass the parameters to the function. In this method, the calling of a function by reference creates an alias of the formal parameters to the actual parameters.
So, this signifies that the called function does not create its own copy of values. Instead, it refers to the original values with the help of reference names. Therefore, the function deals with original data and the changes employed to the original data are reflected back in the referenced data.
Reference parameter characteristics
- A reference can never have a null value. Hence it always refers to a legit object (variable).
- It can not be changed once it is established to make it target to a different object.
- There is no need to implement an explicit mechanism to a reference in order to dereference the memory address and retrieve the actual data value.
Example
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
Conclusion
C++ and Java use both the approaches depending on what is passed. If you want to pass only the value of variable use ‘call by value’ approach, and if you want to see the change in the original value of the variable then use ‘call by reference’ approach.
ncG1vNJzZmislZi1pbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2ScmZyheqPFjK%2BYpa2VYq6vsIycmKWkXZfGbr7En5yrnZ6Ysm%2B006aj