69 759
Assignments Done
99,6%
Successfully Done
In February 2019

Answer to Question #57103 in C++ for priyam

Question #57103
Write the output of the following C++ program code
void Location(int &X,int Y=4)
{
Y+=2;
X+=Y;
}
void main()
{
int PX=10,PY=2;
Location(PY);
cout<<PX<<”,”≪PY<<endl;
Location(PX,PY);
cout<<PX<<”,”≪PY<<endl;
}
Expert's answer
10,8
20,8

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
24.12.18, 19:21

Next step, PX = 10 is passed by reference and PY by value. It means, only PX can change outside Location, PY will remain the same, 10. Changes of PX you can follow as described above.

Assignment Expert
24.12.18, 19:20

Variables, passed by value does not change outside the function, because function works with their copy. Variables, passed by reference will change, because actually function works with the pointer to the original variable.
First you call Location(PY) which means you pass PY by reference. Thus, it takes PY = 2 and uses it as X variable. Inside Location, it increases value of Y: Y := Y + 2 ( = 6) and adds it to the current value of X ( = 2) obtaining 10. But X is the same as PY. Thus, PY becomes 10.

Assignment Expert
24.12.18, 19:20

Function "Location" has two arguments, X passed by reference and Y passed by value. Moreover, Y has default value 4.

Yashita
24.12.18, 17:03

Could u explain ques#57103

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions