Call by value in C++ by R4R Team

Call by value:
-Here call by value refers as we call the function and pass the variable value instead of passing refrence of variable.

program:

#include < iostream>
using namespace std;
class Myclass
{
public:
void swap(int a,int b)
{
int t=a;
a=b;
b=t;
cout<<"After swapping numbers"<< a<<" "<< b<< endl;
}
};
int main()
{
Myclass obj;
int i=10,j=20;
cout<<"Before function calling"<< endl;
cout<<"i and j is: "<< i<<"t"<< j<< endl;
obj.swap(i,j);
cout<<"After function calling"<< endl;
cout<<"i and j is: "<< i<<" "<< j;
return 0;
}


output-

Before function calling
i and j is:
10 20
After swapping numbers 20 10
After function calling
i and j is: 10 20

-In this program, we just pass variable without any refrence just by value, so whatever done in the function are not affect the actual variable which we pass in the function so this kind of concept are called as call by value.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!