#include <iostream>
usingnamespace std;
void mystery(int&, int, int&);
int main()
{
int a = 11, b = 22, c = 33;
mystery(b, c, a);
cout << a << endl;
cout << b << endl;
cout << c << endl;
}
void mystery(int& a, int b, int& c)
{
b += a;
a +=c;
c -= b;
b *= 10;
}
As you can see, in the main function it is labeled as mystery(b, c, a). The output of the code is -443333. I have been working on this awhile now and i'm unable the understand how that output is derived.
The name of the variables in main() are not "linked" to the variables in mystery(). You can see this by renaming the variables in main() or by changing the parameter names in mystery.
int main()
{
int a = 11, b = 22, c = 33;
mystery(b, c, a);
// a == -44, b == 33, c == 33
cout << a << endl;
cout << b << endl;
cout << c << endl;
}
void mystery(int& a, int b, int& c)
{
/* variables in main */
// b c a
/* local variables */
// a == 22, b == 33, c == 11
b += a;
// b == 55
a +=c;
// a == 33
c -= b;
// c == -44
b *= 10;
// b == 550
// however, as b is passed by value
// c in main() doesn't change
}