Online Tutorials & Training Materials | STechies.com
Register Login

Difference Between Call By Value and Call by Reference with Comparison Chart

|| 0

Difference Between Call By Value and Call by Reference with Comparison Chart
Stechies

What is the difference between call by value and call by reference?

Here, we help you take a closer look at the differences between call by value and call by reference. Before understanding the call by value and call by reference difference, it is essential to get acquainted with the definitions and examples of these terms.

Differences Between Call By Value and Call By Reference (Comparision Chart)

The following table will help you understand the differences in call by value and call by reference in C

No.

Call by value

Call by reference

1

Only a copy of the variable’s value is passed the function

The variable value’s address is passed to the function

2

The modifications made to the value of the passed variable present inside the function will be applicable to the function only. The original values of actual parameters will remain unchanged even though the value of the formal parameters may be changed.

Changes made to the value of the variable inside the function will validate outside the function as well. The original values of actual parameters will change when the value of the formal parameters is altered.

3

The formal and actual arguments will be created at different memory locations.

The formal and actual arguments will be created in the same memory location.

4

In the call by value method, the actual arguments remain totally safe. They are incapable of being modified even by accident.

In the call by reference method, alterations to the original arguments becomes possible from within the called function; given this, it is important for coders to handle the program’s arguments carefully to avoid any unexpected results.

What is Call by Value?

In case the programmer passes a primitive data type - which may either be a string, character or an integer – to any method or function, then the value of the primitive data type is passed on to the function code. It is the responsibility of the function to create a copy of the value of an argument and pass it on to a ‘formal parameter’ belonging to the function code. This value is then copied by the function. In case the formal parameter present in the function code faces any modification, then the original value of that argument, which has been used for calling that function, will remain unchanged and not be modified in any way.

In layman’s terms, in case a method or function is called by the ‘call by value’ method, then the variable’s copy is passed on to the function code. In case any changes are made by the function code to the value of the copy of the variable present within it, the original value of the said variable will not change.

Example using Call by Value

Given below is an example to explain the way in which C programming can use the call by value approach to pass arguments.

Consider the function swapvalue() definition as follows:

#include <stdio.h>

/* function declaration */

void swapValue(int x, int y);

int main () {

   /* local variable definition */

   int a = 100;

   int b = 200;

   printf("Before swap, value of a : %dn", a );

   printf("Before swap, value of b : %dn", b );

    /* calling a function to swap the values */

   swapValue(a, b);

    printf("After swap, value of a : %dn", a );

   printf("After swap, value of b : %dn", b );

    return 0;

}

/* Create function to Swap values */

void swapValue(int x, int y) {

 int t;

 t = x; // save the value of x in t variable

 x = y; // put value of y into x  variable

 y = t; // put value of t into y

 return;

}

When the above code is placed in a single C file, compiled and executed, it produces the result as given below:

  • Before swap,value of a: 100
  • Before swap, value of b: 200
  • After swap, value of a: 100
  • After swap, value of b: 200

This result depicts that are no modifications in the values although they have undergone changes within the function code.

What is Call by Reference?

The second method to call a function, the Call by Reference method, allows programmers to pass a reference or address of any argument to the relevant function code. As the argument’s address is passed on to the function code, the parameter accepting the address ‘formally’ is referred to as the ‘pointer’ variable. As the address of a specific argument has been received by the function code, any further changes in the value of the argument will end up modifying the original value of that specific argument in the Call by Reference method of calling a function.

In Java and C++, it is quite commonplace to pass on the object to any method or function; herein, objects are mostly passed by their reference. The modifications made to the objects inside the method or function would affect the specific objects used for invoking that method or function.

Example using Call by Reference

Given below is a small code snippet to exemplify the Call by Reference method of calling upon a function:

#include <stdio.h>

void increment(int  *var)

{

    /* Although the increment is being performed on variable

     * var, however, the var serves to be a pointer holding the address

     * of variable num. This effectively means that the increment is performed on the

     * on the address wherein the value of num variable is stored.

     */

    *var = *var+1;

}

int main()

{
     int num=20;

     /* Here, instead of passing the num variable num, the address of num variable is passed. 
This takes place in the call by reference method.*/

     increment(&num);

     printf("Value of num is: %d", num);

   return 0;

}

The output of this code snippet is:

Value of num is: 21

Example 2

Another example of Call by Reference in C language:

#include<stdio.h>

void swap(int *a, int *b){

 int temp;

 temp=*a;

 *a=*b;

 *b=temp;

}

void main(){ 

 int a=100, b=200; 

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

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

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

}

Output:

  • Value of a: 200
  • Value of b: 100

Call by Value and Call by Reference in Java with Example

Java features only the call by value method of invoking a function/ method; there is no call by reference method in Java. In case a method is called after passing a value, then it is referred to as call by value. The modifications performed in the called function or method will not affect the original value of the variables in the calling method.

Let's take an example of Call by value in Java:

class Operation{ 

 int data=50;   

 void change(int data){ 

 data=data+100;//changes will be in the local variable only 

 }      

 public static void main(String args[]){ 

   Operation op=new Operation(); 

   System.out.println("before change "+op.data); 

   op.change(500); 

   System.out.println("after change "+op.data); 

   } 

} 

Output:

  • Before the change: 50
  • After the change: 50[The value of the variable remains unchanged]

Call By Value vs Call By Reference Key Differences

In Java C++ and Java, when programmers have to call a method or a function, they can do so in two ways. The first way, referred to as call by value is a method in which the actual copy of the value of a variable (only) is passed on to the formal arguments in the relevant function code. In the event of any changes taking place in the value of any variable located inside the function, the original value of the specific variable remains unaffected. The other means of calling a method or function is referred to as call by reference. In this method, the variable is directly passed in an argument. Any change in the value of the variable will affect the variable’s original value.

Conclusion

Java and C++ may use either the call by value approach or call by reference approach for invoking a function as per requirement. In case you desire to pass on the value of any given variable then it is recommended that you use the call by value method. In case you desire a change in the value of the variable as provided to it initially, then it is better to use the call by reference method take your pick!