Difference between pass by value and pass by reference

pass by value:

Pass by value means that a copy of the actual parameter’s value is made in memory. The caller and callee have two independent variables with the same value. If the callee modifies the parameter value, the effect is not visible to the caller.

  • Passes an argument by value.
  • Callee does not have any access to the underlying element in the calling code.
  • A copy of the data is sent to the callee.
  • Changes made to the passed variable do not affect the actual value.

Example:

#include<iostream.h>
#include<conio.h>
using namespace std;
void func(int inVal1, int inVal2) 
{ 
    inVal1 += inVal2++; 
    cout << "In func, inVal1 = " << inVal1 << " inVal2 = " << inVal2 << endl; 
    return;
} 

int main(void) 
{ 
    int num1 = 5, num2 = 7;      
    func(num1, num2); 
    cout << "In main, num1 = " << num1 << " num2 = " << num2 << endl; 
    return 0; 
}

pass by reference:

Pass by reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory. The caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller’s variable. Pass by reference is also called pass by address.

  • Passes an argument by reference.
  • Callee gives a direct reference to the programming element in the calling code.
  • The memory address of the stored data is passed.
  • Changes to the value affect the original data.

Example:

#include<iostream.h>
#include<conio.h>
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 val2 is " << val2 << endl;
   return 0;
}

pass by value vs pass by reference:

pass by valuepass by reference
1. When a method is called, the method arguments reference the same variable in memory as the caller. that is called pass by value. 1. When a method is called, the caller passes a copy of the argument variables to the method resulting in two values in memory, that is called pass by reference.
2. Use pass by value when you are not changing the parameter for the client program.2. Use pass by reference when you are changing the parameter passed in by the client program.
3. Pass by value uses in-mode semantics.3. Pass by reference uses in-mode semantics.
4. In pass by value, the value of each variable in the calling function is copied into corresponding dummy variables of the called function.4. In pass by reference, the address of actual variables in the calling function is copied into the dummy variables of the called function.
5. Values of variables are passed by the Simple technique.5. Pointer variables are necessary to define to store the address values of variables.
6. Pass by value also known as pass-by-copy.6. Pass by reference also called pass by address.
7. This method is preferred when we have to pass some small values that should not change.7. This method is preferred when we have to pass a large amount of data to the function.
8. Pass by value is considered safer as original data is preserved.8. Pass by reference is risky as it allows direct modification in original data

Leave a Reply

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