I don't understand how ampersands work in reference parameters?

I don't understand why my last 3 outputs are like that when I put the ampersand. Can someone make sense of it for me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;

string word = "HI";

void mystery (string s, string t) 
{
t = "HO";
s = s + t;
t = word + s;
cout << s << t;
}

int main ()
{
mystery (word, word);
cout << word << endl;
return 0;
}

// OUTPUT: HIHOHIHIHOHI

// void mystery(string &s, string t)
// OUTPUT: HIHOHIHOHIHOHIHO

// void mystery(string s, string &t)
// OUTPUT: HIHOHOHIHOHOHIHO

// void mystery(string &s, string &t)
// OUTPUT: HOHOHOHOHOHOHOHOHOHOHOHO 
The last 3 outputs are like that due to the fact you are taking in word by reference when you have the ampersand by the variables in the function parameters.

How is that different from the ones without the ampersand?
Well it's different due to the fact that the ones without ampersand sign are taking in word by its value/content. Meaning it is creating a copy of words content and putting that value into the functions parameter with a different address. Now, the ones with the ampersand symbol are taking word in by reference meaning you are taking in the actual variable, e.g both its content and address.

What difference does it make though?
Well makes it pretty big difference in that when you modify a parameter that was taken by reference you are actually modifying the original variable which can be useful if you purposefully want to permanently modify the original contents. And when you modify a variable that was not taken by reference you are not modifying the original variable due to the fact it has a different address from the original.

How does this explain my output?
I'll explain the first and the last one and you can figure out the ones in the middle, lets begin.
For the first one you are taking the values in by value, so you won't be able to modify the original variable(word).

t = "HO" what this does now is simply set the copy t equal to "HO", remember, not modifying the original variable. Now s = s + t is pretty much (HI = HI + HO) which returns "HIHO". t = word + s; is equivalent to ("HO" = "HI" + "HIHO") returning "HIHIHO".

cout << s << t will display "HIHO" then "HIHIHO" which is equivalent to "HIHOHIHIHO".
Finally you display the word "HI" with the statement cout << word << endl. Add those two outputs together and you get:
"HIHOHIHIHOHI" which was the output you got.

Now for the last example both of your parameters are taking in word by reference, so both of the parameters hold words content, and address, pretty much becoming word themselves allowing you to actually modify word.

t = "HO" what this does is set both word AND t to the value of "HO". Not only that but you actually modified s as well due to the fact it too is a reference of word. Remember t is taking word in by reference. Now this line s = s + t pretty much sets both word, s , and t to ("HO" = "HO" + "HO") returning the value "HOHO" to s, t, and word. t = word + s; is ("HOHO" = "HOHO" + "HOHO") returning "HOHOHOHO" to all of the variables.

cout << s << t will now display "HOHOHOHO" then "HOHOHOHO", pretty much ("HOHOHOHOHOHOHOHO").
To which you finally do this cout << word << endl; which will display "HOHOHOHO". In total the output is now "HOHOHOHOHOHOHOHOHOHOHOHO" which is the output you obtained on that function.

I hope this helped you understand why your output was the way it is! Anyways try to decrypt the two in the middle, goodluck!
Last edited on
Topic archived. No new replies allowed.