1

I was wondering if someone could explain why the f2(a,a); is 13 13 and not 12 12?

Does this have something to do with the & sign, if so what does it mean?

Thanks in advance!

#include <iostream>

using namespace std;

void f1 (int a, int b){
 a = 12; b = 13;
}
void f2 (int& a, int& b){
a = 12; b = 13;
}


int main()
{
 int a = 10; int b = 11;
 f1(a,b);
 cout << a << ' ' << b << ' ';
 f2(a,b);
 cout << a << ' ' << b << ' ';
 f2(a,a);
 cout << a << ' ' << b;

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sophia
  • 71
  • 4
  • 5
    Can you explain why you expected it to be `12 12`? – Joseph Sible-Reinstate Monica Oct 20 '21 at 18:30
  • Because in void f2 a is 12, that is why i excpecte it to be 12 – Sophia Oct 20 '21 at 18:33
  • 3
    You pass in `a` to both the `a` and `b` variables of `f2`. These are passed by reference. When it does `a = 12` in the `f2` function, you are indeed changing the value of `a` from `main` to `12` as well; however, immediately after, it does `b=13`, where `b` is still the same variable as `a`. Note that the variables `a` and `b` inside `main` are NOT the same `a` and `b` inside `f2`. – ChrisMM Oct 20 '21 at 18:35
  • 1
    Try reading [How do I use Reference Parameters in C++?](https://stackoverflow.com/q/2564873/10077). – Fred Larson Oct 20 '21 at 18:35

3 Answers3

1

In your call f2(a, a) both arguments refer to the same variable (a) (the ampersand means "reference") so that is all you are changing. The function first assign 12 to it, then you assign 13 to it - so that's the final value. The variable b in the main function is not changed by that function call and retains its value (13), so when you subsequently print a and b in main, you get 13 and 13.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Does having two parameter references that both refer to the same caller's argument have the potential for undefined behavior? And if so, under what circumstances (and is this one of those circumstances)? – Eljay Oct 20 '21 at 18:43
  • @Eljay I think it's only if the two parameters have different types that you get into UB land. But I'm not 100% sure. – Jesper Juhl Oct 20 '21 at 20:49
1

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

By assigning a to f2(a,a); as both parameters you are doing so as a reference parameter and your code is executed sequentially. So a is first given the value 12 (the value assigned to formal parameter a in the function) and then the same variable is given the value 13 (the value assigned to formal parameter b in the function). The variable b in the main part of your program was is 13 after the function call f2(a,b);.

AmaanZA
  • 11
  • 3