1

I have created a C++ program in order to test the functionality of passing parameters by reference for functions.

#include <iostream>

using namespace std;

int f(int &b) {
    b = b + 1;
    cout << b << endl;
    return b;
}

int main() {
    int t = 10;

    cout << f(t) << " " << t << endl;
    //cout << f(&t) << " " << t << endl;

    system("PAUSE");

    return 0;
}

Could you please explain to me why does this program won't affect the value of t after the execution of the f function? The b parameter in passed be reference, so I thought that it's value would change after the execution of the program because I am working with the actual variable from the main function, not a copy of it. In this case, I would expect it to be 11, but it's not affected by the execution of the program.

Why is this happening?

Evan Teran
  • 80,654
  • 26
  • 169
  • 231
Simon
  • 4,681
  • 21
  • 63
  • 92

5 Answers5

5

The value of t does get incremented. You'll see this if you split the output statement in two:

cout << f(t) << endl;
cout << t << endl;

With your original single output statement:

cout << f(t) << " " << t << endl;

the compiler is free to evaluate t before f(t), producing in the output you're seeing. For more info, see cout << order of call to functions it prints?

Community
  • 1
  • 1
NPE
  • 438,426
  • 93
  • 887
  • 970
  • Yes, but why won't it increment? Isn't it passed by reference? Should it be modified within the `main` function as well? – Simon Feb 29 '12 at 16:48
  • It is, just you are not getting the order you expected with all the output on one line. Splitting it over two guarantees things happen in the order you want them. – BoBTFish Feb 29 '12 at 16:50
  • 1
    @Simon: No, it does increment `t`. What happens is that the value that gets **printed** is the value before the function is called. See my updated answer. – NPE Feb 29 '12 at 16:50
  • 1
    @Simon - it is incremented. However, the compiler is allowed to get the value to be output either before or after calling the function. That's why you should output it separately. – Bo Persson Feb 29 '12 at 16:52
0

Your code has undefined behavior, because it is not defined at what point in a statement side-effects take place.

It's similar to

cout << i++ << ++i;

Depending on compiler, you might get different results.

user1071136
  • 15,368
  • 2
  • 35
  • 59
0

I would imagine that you are running into issues involving sequence points. You are modifying and printing the value of t in the same expression.

The order in which the compiler decides to evaluate t and when it decides evaluate f(t) is undefined. So the function simply may not be called first like you would think.

If you break up the printing into two statements, then you will see that t is in fact changed. For example:

cout << f(t) << " ";
cout << t << endl;

would have the output you expect.

Evan Teran
  • 80,654
  • 26
  • 169
  • 231
0

The language doesn't specify the order in which operator arguments are evaluated, so it's unspecified whether your cout expression takes the value of t before or after the function call.

You will see the expected result if you introduce a sequence point, perhaps by splitting it into two expressions:

cout << f(t) << " ";
cout << t << endl;
Mike Seymour
  • 235,407
  • 25
  • 414
  • 617
0

You're tripping over output order. As pointed out, t does get incremented, but you may be confusing yourself over the order of the output.

However, I like to sidetrack from questions a bit and try to figure out how to get people to understand not just the immediate problem but why they are having difficulty in the first place, and here it's pretty clear to me.

You need to learn the debugger.

By using one, you can step through your code and see the value of t at any given point and also see, with hardly any effort on your part, that it does indeed get incremented (including exactly when and how). You'll also just gain a much better understanding of how code works by tracing through it with a debugger including concepts like recursion.

I recommended you get started on this immediately, as it will open your eyes to how the code actually works without guessing your way through it with output statements.

stinky472
  • 6,459
  • 25
  • 27