+ 2

Can somebody please explain this step-by-step?

#include <iostream> int f(int &a, int &b) { a = 3; b = 4; return a + b; } int main() { int a = 1; int b = 2; int c = f(a, a); std::cout << a << b << c; } Why output is 428?

2nd Feb 2017, 4:33 PM
Zernmoram
Zernmoram - avatar
2 Réponses
+ 8
When both arguments are a, you need to remember that the same variable is controlled, and one variable can only hold one value. The function would literally assign 3 to a, and then override the previous assignment with 4 to a, and then return 4+4. Hence the output is 428
2nd Feb 2017, 5:07 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
I got it, thanks!
2nd Feb 2017, 5:34 PM
Zernmoram
Zernmoram - avatar