Skip to main content
Engineering LibreTexts

10.4: Pass by Value vs Pass by Reference

  • Page ID
    29092
  • Argument Passing Techniques in C/C++

    There are different ways in which parameter can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the “caller function” and B is called the “called function or callee function”. Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments.

    Terminology

    • Formal Parameter : A variable and its type as they appear in the prototype of the function or method.
    • Actual Parameter : The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment.
    • Modes:
      • IN: Passes info from caller to calle.
      • OUT: Callee writes values in caller.
      • IN/OUT: Caller tells callee value of variable, which may be updated by callee.

    Important methods of Parameter Passing

    1. Pass By Value : This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment.

      This method is also called as call by value. In the example to the left, the call to geek_func passes the value of 2 arguments, 10, and 12. The copies of the 2 values are known within the function itself as num1 and num2. Assigniing the value 40 to the variabl num1 and 50 to the variable num2 within the geek_func() only affect the variable num1 and num2. Those changes have no impact on the variable val1 or val2...they continue to maintain the values of 10 and 12. C++ is passing the VALUE of val1 and val2...therefore it is just a copy and impacts nothing else.

     

     

     

     

     

     

    // C program to illustrate 
    // call by value 
    #include <iostream> 
    using namespace std;
    
    void func(int inVal1, int inVal2) 
    { 
        inVal1 += inVal2++; 
        cout << "In func, a = " << inVal1 << " b = " << inVal2 << endl; 
        return;
    } 
    
    int main(void) 
    { 
        int num1 = 5, num2 = 7;      // Passing parameters 
        func(num1, num2); 
        cout << "In main, num1 = " << num1 << " y = " << num2 << endl; 
        return 0; 
    } 
    

    The two cout statements show the the changes in the function do NOT impact the original variables in the main() function. 

    In func, a = 12 b = 8
    In main, num1 = 5 y = 7
    1. Pass by reference(aliasing) : This technique uses in/out-mode semantics. Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data. This method is also called as call by reference. This method is efficient in both time and space.

    In the example to the right, we have an integer variable named n, with a value of 10. This variable is used in the function call func(). Notice the ampersand, &, in front of the argument n. This tells the compiler to pass the address of n - in our example that value is memory address 2008.

    Now in the function func() we see the argument variable add has an asterisk in front of it. This syntax says that the variable add is a pointer, that is the value of that variable is an address. So, the statement *add = 20; says, "Set the memory address contained in the variable add, which is 2008, to 20". So the original variable n, which is at memory address 2008, is set to 20.

    We will talk a lot more about addresses and pointers later in the semester...so if you don't understand this completely that's okay at this point.

     

     

     

     

    // C program to illustrate
    // call by reference
    #include <iostream>
    
    void swapnum(int* input1, int* input2)
    {
       int temp = *input1;
       *input1 = *input2;
       *input2 = temp;
       return;
    }
    
    int main(void)
    {
       int val1 = 10, val2 = 20;
    
       // passing parameters
       swapnum(&val1, &val2);
    
       cout << "val1 is " << val1 << " and b is " << val2 << endl;
       return 0;
    } 

    Output:

    val1 is 20 and val2 is 10
    

    Adapted from:
    "Parameter Passing Techniques in C/C++" by Krishna Bhatia, Geeks for Geeks is licensed under CC BY-SA 4.0