Advertisement

Function Call by Value and call by reference C++ - Code with abuzar

   Through this article we are learning about function call by value 

 Function call by value 

method of passing arguments to a function copies the actual value of a arguments into the formal parameters of the function , in case changes made to the parameter inside the function have no effect the argument .


Example 

#include<iostream>
using namespace std ;

// call by value(formal parameters)

 void swap (int a,int b)  
 
{
  int temp ;
  temp = a ;
  a = b;
  b = temp ;
}

int main ()
{
 int a=100 , b=200;

//passing value to function
 swap(a,b); 

 cout<<"Value of a is :"<<a<<endl;

 cout<<"Value of b is : "<<b<<endl;

 return 0;

}

When the above code is compiled then the output is :


value of a is : 100

Value of b is : 200 


Function Call by Reference 

Method of passing argument to a function copies the refrence to an argument into the formal parameter . Inside the function , the refrence is to used to access the actual argument used in the call .


Example 

#include<iostream>
using namespace std ;

voidswapint &a , int &b)   
{
 int temp ;
temp = a ;
 a = b;
 b = temp ;
}

int main ()
{
 int a=100 , b=200;
//passing value to function
 swap(a,b); 

 cout<<"Value of a is :"<<a<<endl;

 cout<<"Value of b is : "<<b<<endl;

 return 0;

}

When the above code is compiled then the output is :

value of a is : 200

Value of b is : 100

 Function call by value & Reference Video : Click Here

 This is my youtube channel : Click Here

 Playlist of C++(Full Course) : Click Here

 PlayList of C++(Program ) : Click Here



Please write comment if you find anything incorrect .

This article is contributed by Mohd Abuzar . if  you like code with abuzar and would like to contribute you can also write an article or mail at abuzar.abuzar.mohd325@gmail.com .




Post a Comment

0 Comments