Difference between Call by Value and Call by Reference

In programming languages, functions can be invoked in two ways: which is known as Call by Value and Call by Reference.

In this tutorial, you will learn,

What is Call by Value method?

Call by value method copies the value of an argument into the formal parameter of that function. Therefore, changes made to the parameter of the main function do not affect the argument.

In this parameter passing method, values of actual parameters are copied to function's formal parameters, and the parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.

KEY DIFFERENCE

  • In Call by value method original value is not modified whereas, in Call by reference method, the original value is modified.
  • In Call by value, a copy of the variable is passed whereas in Call by reference, a variable itself is passed.
  • In Call by value, actual and formal arguments will be created in different memory locations whereas in Call by reference, actual and formal arguments will be created in the same memory location.
  • Call by value is the default method in programming languages like C++, PHP, Visual Basic NET, and C# whereas Call by reference is supported only Java language.
  • Call by Value, variables are passed using a straightforward method whereas Call by Reference, pointers are required to store the address of variables.

What is Call by Reference method?

Call by reference method copies the address of an argument into the formal parameter. In this method, the address is used to access the actual argument used in the function call. It means that changes made in the parameter alter the passing argument.

In this method, the memory allocation is the same as the actual parameters. All the operation in the function are performed on the value stored at the address of the actual parameter, and the modified value will be stored at the same address.

Example of a Call by Value method

void main() {
        int a = 10,
        void increment(int);
        Cout << "before function calling" << a;
        increment(a);
        Cout << "after function calling" << a;
        getch();

        void increment(int x) {
            int x = x + 1;
            Cout << "value is" << x;
        }

Output:

before function calling 10
value is 11
after function calling 1-0

Because variable declared 'a'in main() is different from variable 'x' in increment(). In this programme only variable names are similar, but their memory address are different and stored in different memory locations.

Example of a Call by Reference method

Public static void(string args[]) {
    int a = 10;
    System.out.println("Before call Value of a = ", a);
    Void increment();
    System.out.println("After call Value of a = ", a);
}

Void increment(int x) {
    int x = x + 1;
}

Output:

Before call Value of a =10
After call Value of a =11

Because variable declared 'a' in is referencing/ pointing to variable 'a' in main(). Here variable name is different, but both are pointing/referencing to same memory address locations.

Call by Value vs. Call by Reference

Parameters Call by value Call by reference
Definition While calling a function, when you pass values by copying variables, it is known as "Call By Values." While calling a function, in programming language instead of copying the values of variables, the address of the variables is used it is known as "Call By References.
Arguments In this method, a copy of the variable is passed. In this method, a variable itself is passed.
Effect Changes made in a copy of variable never modify the value of variable outside the function. Change in the variable also affects the value of the variable outside the function.
Alteration of value Does not allow you to make any changes in the actual variables. Allows you to make changes in the values of variables by using function calls.
Passing of variable Values of variables are passed using a straightforward method. Pointer variables are required to store the address of variables.
Value modification Original value not modified. The original value is modified.
Memory Location Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in the same memory location
Safety Actual arguments remain safe as they cannot be modified accidentally. Actual arguments are not Safe. They can be accidentally modified, so you need to handle arguments operations carefully.
Default Default in many programming languages like C++.PHP. Visual Basic NET, and C#. It is supported by most programming languages like JAVA, but not as default.

Advantages of using Call by value method

Pros/benefits of a call by value method:

Advantages of using Call by reference method

Pros of using call by reference method:

Disadvantages of using Call by value method

Here, are major cons/drawbacks of a call by value method:

Disadvantages of using Call by reference method

Here, are major cons of using call by reference method:

 

YOU MIGHT LIKE: