Simple example of Function in C++, which explain the working of return by reference:
Program Code:
#include <iostream>
using namespace std;
int & hello(int &x)
{
x=x+10;
cout<<x;
return x;
}
int main()
{
int x;
cin>>x;
cout<<x;
cout<<endl;
hello(x)=200;
cout<<endl;
cout<<x;
}
using namespace std;
int & hello(int &x)
{
x=x+10;
cout<<x;
return x;
}
int main()
{
int x;
cin>>x;
cout<<x;
cout<<endl;
hello(x)=200;
cout<<endl;
cout<<x;
}
The output of the program is tested on www.jdoodle.com
Output:
10
20
200
20
200
Thanks
Mukesh Rajput
Post A Comment:
0 comments: