2 min read

Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters.

Call By Value:

In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of caller.By default, C++ uses call by value to pass arguments.

#include <iostream.h>
void change(int data); 
int main()
      { 
        int data = 3; 
        change(data); 
        cout << "Value of the data is: " << data<< endl;  
        return 0; 
}
void change(int data) 
{
data = 5;
} 

Output

Value of data is:5

Call by Reference:

Both the actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of caller.

Bade Kaam ki baat (BKKB): To understand the call by reference, you must have the basic knowledge of pointers.

Let’s try to understand the concept of call by reference in C++ language by the example given below:

#include<iostream> 
using namespace std;
void swap(int *x, int *y) 
{
   int swap; 
   swap=*x; 
   *x=*y; 
   *y=swap; 
}
int main() 
{ 
   int x=500, y=100; 
   swap(&x, &y);  // passing value to function 
   cout<<"Value of x is: "<<x<<endl; 
   cout<<"Value of y is: "<<y<<endl;
   return 0; 
} 

OUTPUT

Value of x is:100
Value of y is:500

DIFFERENCE BETWEEN CALL BY VALUE AND CALL BY REFERENCE

Difference between call by value and call by reference
ParametersCall by ValueCall by Reference
BasicA copy of the variable is passed.A variable itself is passed.
effectChange in a cop of variable doesn’t modify the original value of variable.Change in a copy of variable modify the original value of variable.
Syntax
function_name(variable_name1,variable_name2…)function_name(&variable_name1,&variable_name2…)
Default CallingPrimitive types are passed using “call_by_value”.Objects are implicitly passed using “call_by_reference”.

If you liked my article please follow me on @priya123 to get the latest articles and post by me.

Bookmark(0)
Categories: Uncategorized

3 Comments

Okon · September 5, 2019 at 8:52 pm

Wonderful blog! Do you have any helpful hints for aspiring writers? I’m hoping to start my own blog soon but I’m a little lost on everything. Would you propose starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m completely confused .. Any recommendations? Many thanks!

    Priya · September 18, 2019 at 1:21 pm

    Comment Hii okon, thanks for your response. I think it totally depends on you, whether you want to do writing for free or want it to be paid. In any of the way you can improve your skills.

Leave a Reply