-4
#include<iostream>
int fun(int &x,int y=10)
{
 if(x%y==0)
  return (++x);
 else
  return (y--);
}
int main()
{
 int p=10,q=13;
 q=fun(p,q);
 cout<<p<<q<<endl;
 q=fun(q);
 cout<<p<<q<<endl;
 q=fun(p);
 cout<<p<<q<<endl;
 return 0;
}

The output as shown is as follows:

1013
1010
1111

But when I try to solve it using pen and paper I am stuck. Please show me how to arrive at the correct output so as to keep my concepts clear.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

p=10 q=13

1st call p= 10 , q = 13 p is passed by reference

returned will be y-- since it is post decrement it will return 13

2nd call

q = 13 passed by ref x = 13 y =10

return will be y-- which is =10 not 9 since it is post

now p = 10 and q =10

3rd call x = 10 and y =10 by default return ++x and x = 11 ,so q=11,now since its is call by ref x =11 so p=11 and