int& question

I'm not sure if I understand this properly and can't find it anywhere.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  void cat (int a, int b, int& c)
int main ()
{
int w = 1; 
int x = 2; 
int y = 3; 
int z = 4; 

cat (w, x, y); 
}

void cat(int a, int b, int& c)
{
a += 2;
c= a+b;
}


Does that make w=3, x=2, y=5 and z=4?
This is called pass-by-reference and it is the same as if you had done this:

1
2
3
4
5
6
7
8
9
10
11
void cat (int a, int b, int* c)
int main () {
    int w = 1, x = 2, y = 3, z = 4; 
    cat (w, x, &y); 
    return 0;
}

void cat(int a, int b, int* c) {
    a += 2;
    *c = a + b;
}


In both cases,
w = 1, x = 2, y = 5, z = 4
Last edited on
Topic archived. No new replies allowed.