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;
}
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
Thank you! Your comments have been successfully added. However, they need to be checked by the moderator before being published.
Comments
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.
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.
Function "Location" has two arguments, X passed by reference and Y passed by value. Moreover, Y has default value 4.
Could u explain ques#57103
Leave a comment