0
void addOne(int &y)
{
    y = y + 1;
}

int main()
{
    int x = 5;
    std::cout << "x = " << x << '\n';
    addOne(x);
    std::cout << "x = " << x << '\n';
    return 0;
}

Sorry if this is a basic question. But why does y = y + 1 is 6?

Student
  • 432
  • 2
  • 10
  • 30
  • You might want to get a [decent book on C++](http://stackoverflow.com/q/388242/253056) and read the chapter on *references*. – Paul R Mar 13 '16 at 08:00
  • That is what I did. But I really dont get this part in the notes. That is why I need more people to assist me here to understand better..sorry – Student Mar 13 '16 at 08:01
  • `addOne` just adds 1 to whatever `int` variable you pass to it (*by reference*). So in this case you pass `x`, which has a value of 5, and it has 1 added to it to become 6. – Paul R Mar 13 '16 at 08:04
  • I see. But why does the computer ignore the variable y infornt? Wont it be like 5 = 5 + 1? – Student Mar 13 '16 at 08:09
  • 1
    This is just how assignment works - the right hand side is first evaluated, and then the result of this evaluation is *assigned* to the left hand side. So it's `y = 5 + 1`. – Paul R Mar 13 '16 at 08:12
  • 1
    do you understand `x = 7;` for example? – M.M Mar 13 '16 at 09:45

2 Answers2

2

What might help, initially, is to think of references as syntactic sugar for pointers.

Consider the following:

int main() {
    int x = 5;
    int *px = &x;
    *px = 8;

    cout << x << endl;

    return 0;
}

This prints out 8, as you would expect. (If you didn't expect that then it might be worth reading an introduction to pointers before tackling references.)

A reference variable, then, can be thought of as a pointer variable whose syntax is that of a regular variable. The following is equivalent to the previous example:

int main() {
    int x = 5;
    int &rx = x;
    rx = 8;

    cout << x << endl;

    return 0;
}

That is, rx becomes a reference to x, and thus whenever you change rx, you change x as well, because both names refer to the same thing... or in other words, rx is bound to a memory location, not a value, and setting rx changes what is at that memory location.

In a sense, rx is sort of invisible - it doesn't have an existence of its own, it only exists in terms of what x is (which is why you have to initialize a reference when it's declared, i.e. you can't do int &rx; in a function and leave it hanging).

So when you have:

void addOne(int &y)
{
    y = y + 1;
}

This is a function whose parameter is a reference to what is passed in... and so when the parameter reference is changed, that which the reference refers to is also changed.

And so when you call it like this:

int main()
{
    int x = 5;
    std::cout << "x = " << x << '\n';
    addOne(x);
    std::cout << "x = " << x << '\n';
    return 0;
}

This calls addOne with x, thus initializing the int & reference y to x, and so whenever y is changed, x is changed... and so x changes from 5 to 5 + 1, i.e. 6.

You can think of it as equivalent to the following:

void addOne(int *py)
{
    *py = *py + 1;
}

int main()
{
    int x = 5;
    std::cout << "x = " << x << '\n';
    addOne(&x);
    std::cout << "x = " << x << '\n';
    return 0;
}

To see why you'd use a reference vs. a pointer, see this question.

Community
  • 1
  • 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1

A reference is not an independent variable, you should consider it to literally be another. So when you pass x to function addOne, the reference called y literally becomes x (how this works behind the screens is less important right now).

So when you increase y by one, what you are really doing is increasing x. That's why x is 6, after the call to addOne.

H. Guijt
  • 3,325
  • 11
  • 16
  • When 5 is passed, the equation will be 5=5+1? Or I am wrong? – Student Mar 13 '16 at 08:05
  • You are not doing math, you are programming. In that expression you tell the machine to take the _variable_ that currently holds value 5, add 1, and store the result back into the same variable. The fact that some notation is shared with mathematical expressions does not make a computer language into a direct expression of the same concepts you have in math, and you should not interpret it in the same way at all. – H. Guijt Mar 13 '16 at 08:06
  • I see. So the computer will ignore the value 5 passed to the variable itself? – Student Mar 13 '16 at 08:08
  • 1
    Hardly. But I think you are struggling very much with extremely basic concepts. Again, don't treat this as a set of mathematical equations that should somehow balance out to be true. Instead, read it as a series of mechanical steps the machine should take, in order. You tell the machine there is a memory location that you call x. You tell it that memory location should have value 5. Then you tell it to increase that memory location by one. – H. Guijt Mar 13 '16 at 08:10
  • Okay..I think I slowly understand what you meant by now. I will practice this more on pc and see what other outcome i get – Student Mar 13 '16 at 08:11
  • @Student `5` is not passed to anything. When you write `y` it means *a variable called `y`*, it does not mean the contents of that variable. What you do with the variable depends on what you do with it. If you go `y + 1` then it retrieves the value stored in the variable so it can add 1. If you go `y = 9` then it stores a new value in the variable, without retrieving the old stored value. – M.M Mar 13 '16 at 09:47
  • replace the word "variable" with "memory location" if it helps you to understand – M.M Mar 13 '16 at 09:48