This function
void f2 (int& a, int& b){
a = 12; b = 13;
}
accepts its arguments by reference. So calling it like
f2(a,a);
the both parameters are references to the same object a
.
At first this object was assigned with the value 12
a = 12;
and then reassigned with the value 13
b = 13;
So the resulting value of the referenced object a
is 13
.
To make it more clear you can imagine the function call and its definition the following way (I will rename the parameters as a1 and b1 to avoid name collision)
f2(a,a);
// ...
void f2 ( /* int& a1, int& b1 */ ){
int &a1 = a;
a1 = 12;
int &b1 = a;
b1 = 13;
}
So the object a
is being changed through the reference to it a1
and b1
.
That is you may declare several references to the same object and use the references in any order to change the referenced object. The object will contain the last value assigned to it through one of the references.
Pay attention to that before this call
f2(a,a);
there was the following call
f2(a,b);
that set the value of the variable b
to 13
.
So this statement after the above two calls
cout << a << ' ' << b;
outputs
13 13