Passing by reference

Hello,

I'm selftaught, and sometimes I have the feeling I really missed something on my way...

When I want to pass an argument by reference, I would code this as following:

1
2
3
4
5
6
void someFunction(int* someVar);

//...

int a;
someFunction(&a);


At it would work fine. But I came around the following construction, and I really don't understand what's going on here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

void f(int& a)
{
    a = 6;
}

int main()
{
    int b = 5;
    f(b);
    cout<<b;
    cin.ignore();
    
    return 0;
}


What does the '&' after int do? Why has b the value 6 after calling f()? I passes its value, not its memory-addres, right?
You answer is "reference variable"
Just google "C++ reference variable" and you'll get the answer to your question. :)

Hope this helps !
example: void f ( int a, int &b, int *c )
'a' is passed by value, 'b' by reference and 'c' by address
http://www.cplusplus.com/doc/tutorial/functions2/
Thanks!

I have read the whole tutorial, but forgot this because I never used it... I tried to find the answer in the tutorial, but I searched in the page about pointers. Thanks for your help.
Topic archived. No new replies allowed.