call by reference – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Fri, 26 Aug 2022 20:15:44 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg call by reference – Programmerbay https://programmerbay.com 32 32 Difference Between Call By Value and Call by Reference in Tabular Form https://programmerbay.com/difference-between-call-by-value-and-call-by-reference-in-tabular-form/ https://programmerbay.com/difference-between-call-by-value-and-call-by-reference-in-tabular-form/#respond Sun, 10 Jul 2022 17:45:13 +0000 https://www.programmerbay.com/?p=4216 In programming, a function is a set of statements that carries out a specific task. It increases the code readability and reusability.
In order to use a function, one requires to invoke it by using its name and parameters.
A function can be called by using two ways :
  1. Call by reference
  2. Call by value

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 Value

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 reference

Difference between call by value and call by reference in Java in tabular form

CALL BY VALUECALL BY REFERENCE
In this, a value of a variable is passed as an argument to a function's formal parametersIn 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 memoryA 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 methodChanges made to reference variable within the local function is reflected outside the method
Value of a variable is passed to the argumentPointer variable that is used to store the address, is passed to the formal argument
Original value doesn't get changedOriginal value gets changed

Call by Value

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.

C program to show the working of Call by Value

#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

Call by reference

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

Call by reference and Call by value in Java

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.

  1. Primitive data type : In this, a primitive datatype is passed as an argument to a method’s formal parameters
  2. Object reference type: In this, a reference type is passed as an argument to a method’s formal parameters

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”.

Java program to demonstrate how call by value and call by reference works?

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

]]>
https://programmerbay.com/difference-between-call-by-value-and-call-by-reference-in-tabular-form/feed/ 0