• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Call By Value and Call by Reference

Call By Value and Call by ReferenceCall 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

  1. Comparison Chart
  2. What is Call By Value?
    • Advantages of Call by Value
    • Example
  3. What is Call By Reference?
    • Example
  4. Key Differences
  5. Conclusion

Comparison Chart

Basis for ComparisonCall By ValueCall By Reference
Basic

A copy of the variable is passed.A variable itself is passed.
EffectChange 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 Parametersfunction_name (variable_name1, variable_name2, . . . .);function_name (&variable_name1, &variable_name2, . . . .);
//in case of object
object.func_name( object);
Receiving Parameterstype 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 callingPrimitive 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 locationActual and formal argument are created in same memory location
SecureActual 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.

Note: With the call-by-value approach, we can pass variables with the primitive data type (integer, character, and string ) only.

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.

Note: We pass reference variables to the calling function with the ‘call by reference’ method. This method 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 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Related Differences:

  1. Difference Between Actual and Formal Parameters
  2. Difference Between Pointer and Reference
  3. Difference Between ref and out in C#
  4. Difference Between Structure and Union
  5. Difference Between int and long

Comments

  1. Avinal Kumar says

    November 19, 2018 at 8:11 am

    I read few comparisons and wow they are amazingly explained. Nice work.

    Reply
  2. Aiswaryaa says

    March 1, 2022 at 5:38 am

    Amazing explanation

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy