简体   繁体   中英

C++ - passing parameter by reference in a function which returns int

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?

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?

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.

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM