Kindly explain the following program how will be executed. Why Y value is assigned -1?
#include<iostream.h>
#include<conio.h>
int &maxref(int &a, int &b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
int x=20, y=30, max=0;
maxref(x,y)=-1;
cout<<"Value of x is:"<,x;
cout<<"Value of y is:"<<y;
getch();
Well, your function returns a reference to a integer, which means an lvalue, which means that it can be modified, and in your case that value will be y. Assigning to your function -1, your are modifying the value of y. Just try to make x greater than y and you will see that x == -1 !
Well, your function returns a reference to a integer, which means an lvalue, which means that it can be modified, and in your case that value will be y. Assigning to your function -1, your are modifying the value of y. Just try to make x greater than y and you will see that x == -1 !
Your functions is an lvalue, the result of the internal body, that can be modified with this expression: maxref(x,y)=-1;//maxref(x, y) is an lvalue,
//it's on the left of the operator =, that means that its result can be modified!