Call by Value vs Call by Reference: All Important Differences

Posted in /   /  

Call by Value vs Call by Reference: All Important Differences

Swapnil Banga
Last updated on November 15, 2022

    A function in programming is a segment of code that performs a particular operation. There are two ways to call a function in a program : call by value and call by reference. This article discusses the differences between the two, i.e., call by value vs call by reference. In every high-level programming language, there is support for user-defined functions, and functions are used to perform specific tasks. A function provides modularity to the program and increases the reusability of code. Instead of writing similar code, again and again, we can define a function that can perform that task and just invoke (or call) that function whenever we need it. Let's know how.

    Call by Value vs Call by Reference

    In a programming language, we can invoke a function in two ways:

    1. Call by Value.
    2. Call by Reference.

    When we call a function, we can pass arguments along with it, provided the function accepts arguments. Arguments that we pass to the function during the function calling are actual arguments, and the user-defined function accepts these as formal arguments. The call by value method and call by reference method differs on how the formal parameter accept the values from the actual parameters. Here in this article, we will compare the two ways of invoking or calling a function. Check out the following code example: A Functional Calling Example in C++:

    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    int func(for_arg_1, for_arg_2)   // here func arguments are formal arguments
                    { return for_arg_1+for_arg_2;
                    }
    void main()
    {
    int act_arg_1=1, act_arg_2=2;
    clrscr();
    cout<<func(act_arg_1,act_arg_2);  //here func arguments are actual arguments
     getch();
    }

    Output:

    3 

    1) Call by Value Method

    In the call by value method, the formal arguments accept the copies of the actual arguments, and the function performs operations on the formal argument or copies of the actual arguments. That’s why all the changes made to the formal arguments do not have any effect on the actual arguments. In this method, when we call the function, the function first creates the copies of the actual arguments in different memory locations and then sends these copies to the formal arguments. Here, if the function makes any changes in the formal arguments, then the changes do not show any adverse effect on the actual arguments. A Call by Value Method Example in C++:

    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    int func(for_arg_1, for_arg_2)
                    { 
                    for_arg_1=45;
                    for_arg_2=70;
                    cout<<"\nThe formal argument 1 has been changed to: "<< for_arg_1;
                    cout<<"\nThe formal argument 2 has been changed to: "<<for_arg_2<<endl;
                     }
    void main()
    {
    int act_arg_1=1, act_arg_2=2;
    clrscr();
    cout<<"Before we call the function the value of both actual arguments is:";
    cout<<"\nThe value of actual argument 1 is: " <<act_arg_1;
    cout<<"\nThe value of actual argument 2 is: " <<act_arg_2;
    cout<<"\n\n\n---------------------Using Call by Value method-------------------" ;
    func(act_arg_1,act_arg_2);
    cout<<"-------------------------------------------------------------------" ;
    
    cout<<"\n\n\nAfter calling the function the values of actual arguments is:\n ";
    cout<<"\nThe value of actual argument 1 is: "<<act_arg_1;
    cout<<"\nThe value of actual argument 2 is: "<<act_arg_2;
     getch();
    }

    Output:

    Before we call the function the value of both actual arguments is:
    
    The value of actual argument 1 is: 1
    The value of actual argument 2 is: 2
    
    ---------------------Using Call by Value method-------------------
    
    The formal argument 1 has been changed to: 45
    The formal argument 2 has been changed to: 70
    
    -------------------------------------------------------------------
    
    After calling the function the values of actual arguments is:
    
    The value of actual argument 1 is: 1
    The value of actual argument 2 is: 2

    Advantages:

    • It does not change the value of the actual parameter. Thus, it keeps the code simple.
    • No matter how many times the function gets called, there will be no effect on the actual arguments.

    Disadvantages:

    • For every actual parameter, it creates a copy that occupies memory space.
    • We have to pass the argument as a variable.

    2) Call by Reference Method

    In the call by reference method, the formal parameters accept the address of the actual parameters, and here we use pointers to grab the value associated with those addresses. Hence, here we are using the same address for the formal and actual arguments. Thus, if any change occurs in the value of the formal parameters, then that will also change the value of the actual arguments. Hence, in this method, addresses are the same for both formal and actual arguments. The conflict of changing values is clearly visible. A Call by Reference Method Example in C++:

    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    int func(int *for_arg_1, int *for_arg_2)
                    { *for_arg_1=45;
                    *for_arg_2=70;
                    cout<<"\nThe formal argument 1 has been changeed to: "<< *for_arg_1;
                    cout<<"\nThe formal argument 2 value has been changed to: "<<*for_arg_2<<endl;
                     }
    void main()
    {
    int act_arg_1=1, act_arg_2=2;
    clrscr();
    
    cout<<"Before we call the function the value of both actual arguments is:";
    cout<<"\nThe value of actual argument 1 is: " <<act_arg_1;
    cout<<"\nThe value of actual argument 2 is: " <<act_arg_2;
    
    cout<<"\n\n\n---------------------Using Call by Reference method-------------------" ;
    func(&act_arg_1,&act_arg_2);
    cout<<"-------------------------------------------------------------------" ;
    
    
    cout<<"\n\n\nAfter calling the function the values of actual argumetns is:\n ";
    cout<<"\nThe value of actual argument 1 is: "<<act_arg_1;
    cout<<"\nThe value of actual argument 2 is: "<<act_arg_2;
     getch();
    }

    Output:

    Before we call the function the value of both actual arguments is:
    
    The value of actual argument 1 is: 1
    The value of actual argument 2 is: 2
    
    ---------------------Using Call by Reference method-------------------
    
    The formal argument 1 has been changed to: 45
    The formal argument 2 has been changed to: 70
    
    -------------------------------------------------------------------
    
    After calling the function the values of actual arguments is:
    
    The value of actual argument 1 is: 40
    The value of actual argument 2 is: 70

    Advantages:

    • It is ideal when we want to change the value of the actual parameter along with formal arguments.
    • Unlike the call by value method, it does not create any copies, which makes it more memory efficient.

    Disadvantages:

    • The use of memory addresses in the function makes the program complex.
    • It can manipulate the actual parameters too, which can create a problem.

    Call by Value vs Call by Reference: A Head-to-Head Comparison

    The following table enumerates the various differences between the two ways of calling a function:

    Call by Value Call by Reference
    It sends a copy of the actual parameters to the formal parameters. It sends the address of actual parameters to the formal parameters.
    All the changes occur in the copies. Hence, it does not have any effect on the actual parameters. All the changes have a direct effect on the value of the actual parameters.
    When we call the function, we pass the value of the actual parameters. e.g. function(actualparameter1 , actualparameter2 ) When we call the function, we send the address of the actual parameters. e.g. function(&actualparameter1 , &actualparameter2)
    The function receives the calling as copies. e.g. function(formalparametes1, formalparameters2) The function receives the calling as addresses. e.g. function(*formalparametes1, *formalparameters2)
    We pass primitive types in the call by value method. We pass objects implicitly in the call by reference method.

    Conclusion

    That concludes our discussion on call by value vs call by reference. Both function calling methods have their advantages over one another. Hence, you must know when to use which way of function calling to optimize your program. People are also reading:

    Leave a Comment on this Post

    0 Comments