0

Help me please to understand code

int x(int &a,int &b){ a=1; b=3; return a*b; } int main() { int a=3; int b=4; int c=(b,b) ; cout<<a<<b<<c;

2nd Aug 2017, 4:51 PM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
3 Answers
+ 7
Variable b passed via reference into both parameters of function x. In function x, original passed value is 4. Altered to 1 on first line, then altered to 3 on second line. Returns 3*3. Hence, local variable c is 9. The key here is to understand that in function x, a and b refers to the same variable, because both parameters were filled with local variable b, which was passed via reference. Final output : 339
2nd Aug 2017, 5:24 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
I dont to disapoint you, but output 344, i checked))
3rd Aug 2017, 5:37 AM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
+ 1
That's why im asking
3rd Aug 2017, 5:37 AM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar