Use of References

Hi,

I have this code below and was wondering why the outcome of the cout becomes 576. I worked it step by step and ended up with 654. I got the first line right the first time you call the function f(x,y,z) and got 356, but after that I'm completely lost. Could someone please explain to me in detail line by line when calling the function f and how its calculated?

Thanks.

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

void f(int a, int &b, int &c)
{
	a++; b++; c++;
}


int main()
{
	int x = 3;
	int y = 4;
	int z = 5;

	f(x,y,z);
	f(x,x,y);
	f(z,y,x);
	
	cout << x << y << z;

	system ("pause");
	return 0;
}
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void f(int a, int &b, int &c)
{
	a++; b++; c++;
}


int main()
{
	int x = 3;
	int y = 4;
	int z = 5;

	f(x,y,z); //x=3, y=5, z=6
	f(x,x,y); //x=3, x=4, y=6
	f(z,y,x); //z=6, y=7, x=5
	
	cout << x << y << z;

	system ("pause");
	return 0;
}
Topic archived. No new replies allowed.