What does the ' return x ' do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What does the ' return x ' do?

int func (int&x, int&y, int&z){ x=7; y=8; z=9; return x; } int main (){ int a=1, b=2, c=3; c=func(b,a,c); cout<<a<<b<<c; return 0; } // outputs 877 Based on my understanding, x is b's reference, y is a's reference, and z is c's reference. As references x, y and z are assigned new values, so are b, a and c (7, 8 and 9 respectively). Then comes in a ' return x ' What does ' return x ' do here? I'd expected 879 instead of 877. What happened to the 9?

26th Jul 2020, 8:51 AM
Solus
Solus - avatar
1 Answer
+ 4
c is reassigned as the value of output of the function (c=func(a,b,c)), which is 7. My hunch is that the function does exactly as you'd expect, but you had just missed this little detail.
26th Jul 2020, 9:03 AM
Russ
Russ - avatar