Call by value : Where a copy of the original value is created and passed in the formal parameters of a method. In this, if any changes are made to formal arguments within the local method would not be reflected in the original data.
Call by reference : Where a copy of the address is passed to formal argument which points to the address of the original value .
As a result, if any change is made through that copied reference variable would directly reflect on actual data.
CALL BY VALUE | CALL BY REFERENCE |
---|---|
In this, a value of a variable is passed as an argument to a function's formal parameters | In this, an address of a variable is passed as an argument to a function's formal parameters |
A separate memory is allocated to a new variable holding copied value in the stack memory | A separate memory is allocated to a new variable pointing to the address of another variable |
Changes made to the cloned value within the local function is not reflected outside the method | Changes made to reference variable within the local function is reflected outside the method |
Value of a variable is passed to the argument | Pointer variable that is used to store the address, is passed to the formal argument |
Original value doesn't get changed | Original value gets changed |
In this, a copy of the actual value is generated and passed in the formal parameter of a method. Any change made in those copied value would not affect the original one because both copied and original values are stored in different location.
Original values are copied from the actual parameter to the formal parameter of a function, meaning, variables in both parameters point to different memory spaces. Therefore, if one changes the value of a variable passed to the formal parameter, then it would not be reflected outside the function.
#include <stdio.h> void sum (int x); int main () { int a = 10; printf ("before sum function called : %d \n", a); sum (a); printf ("After sum function called : %d \n", a); return 0; } void sum (int x) { x = x + 20; printf ("sum : %d \n", x); }
Output:
before sum function called : 10 sum : 30 After sum function called : 10
In this, a copy of the reference variable is generated and passed in the formal parameter of a method. Any change made through that particular copied variable would affect the actual data because both copied and original reference variable are pointing to same memory location where data is located.
An address is passed from the actual parameter to the formal parameter of a function, meaning, variables in both parameters point to the same memory space. Therefore, if one changes the value of a variable passed in a formal parameter would be reflected outside the function.
#include <stdio.h> void sum (int *x); int main () { int a = 10; printf ("before sum function called : %d \n", a); sum (&a); printf ("After sum function called : %d \n", a); return 0; } void sum (int *x) { (*x) = (*x) + 20; printf ("sum : %d \n", (*x)); }
Output:
before sum function called : 10 sum : 30 After sum function called : 30
In C++, Call by reference can be achieved using pointers that can be defined as a variable that is capable of storing an address of other variables.
Unlike C++, Java doesn’t support pointers due to security reasons. likewise, call-by-reference is not possible in Java.
Java only supports “call by value”, in the case of objects, it acts differently.
In Java, there are two types of arguments.
Object reference :
An object reference can be defined as memory location which refers to the actual object’s information i.e methods, and variables. The variable that holds it is termed an object reference variable.
In Java, a copy of an object reference is passed from the actual parameter to the formal parameter where both variables passed in the arguments are pointing to the same object. As a result, changes made on the passed variable inside the method is reflected in an actual variable.
In Java, Call by value works by copying the value of an actual parameter to the formal parameter of a method. In this, the value of an actual argument is copied to a different memory space and assigned to the formal argument of a method.
In call by value, any operation performed on the received value, which is a copy, inside a method would not impact the original value residing in the caller method. This is also termed “pass by value”.
Program:
public class callme{int x; callme(){ x=20; } void callByValue(int x) { x=x+10; // try to make changes } void callByReference( callme obj) { obj.x = obj.x + 10; // try to make changes } public static void main(String[] args) { callme o = new callme(); // to demonstrate how call by reference works int data=10; // to demonstrate how call by value works System.out.println("Actual Value of 'data' variable before passing it as a parameter = "+data); System.out.println("Actual Value of object that 'o' reference variable pointing to before passing it as a parameter = "+o.x); o.callByValue(data); // passing permitive type o.callByReference(o); // getting reference type System.out.println("Actual Value of 'data' variable after changes = "+data); System.out.println("Actual Value of object that 'o' reference variable pointing to after passing it as a parameter = "+o.x); } }
Output:
Actual Value of 'data' variable before passing it as a parameter = 10 Actual Value of object that 'o' reference variable pointing to before passing it as a
parameter = 20
Actual Value of 'data' variable after changes = 10 Actual Value of object that 'o' reference variable pointing to after passing it as a
parameter = 30
This post was last modified on August 26, 2022