Output Writing – Function Returning reference – CSKC
Sign In

Search

Solved Problem #CPP#5265    siteicon   siteicon  

Problem Statement - Output Writing – Function Returning reference

Write output of the following code. Assume inclusion of related header files.

int &min(int &a, int &b)
{
  if (a<b) return a;
  else return b;
}

int main()
{
  int x=2,y=3;
  min(x,y)=10; 
  cout<<"x-y = "<<(x-y)<<endl;
  cout<<"y-x = "<<(y-x)<<endl;
  return 0;
}

Solution

TC++ #5265

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

Notes

int &min(int &a, int &b)   if (a<b) return a; else return b;
Returns reference to a or b whichever is lower by its value content.

int x=2,y=3; 
min(x,y)=10;
Left side is supposed to be a function, since the function returns a reference of memory location it has been placed on left side. The returned reference will be filled with value 10. In this case x is lower so x will be filled with 10.

cout<<“x-y = “<<(x-y)<<endl;
Since x is now 10 and y is 3 so this will print 7.

cout<<“y-x = “<<(y-x)<<endl;

Since  y is 3 and x is 10 so this will print -7.


Share

Created: 4-Dec-2018 20:57 | Updated: 22-Mar-2026 20:17|