简体   繁体   中英

Why does function Two return cout << s << " " << t << " " << b << endl; when it says to only return (b)?

Hello I need help with this tracing problem, here here is a copy of the problem:

What is the output of the following program segment?

int u = 4, v = 3;
one(u, v);
cout << u << " " << v << endl;
cout << two(u, v) << " " ;
cout << u << " " << v << endl;
void one(int &x, int& y){
 int a;
 a = ++x ;
 x += y++;
 y = ++a;
}
int two(int s, int &t){
 int b;
 b = s – t;
 s += t + b ;
 t += 4 * b;
 cout << s << " " << t << " " << b << endl;
return ( b ) ;
}

I managed to find the first output of function One, then I plugged it into function Two to find its output. But function Two returned the cout instead of the return (b), can anyone show me what I am doing wrong, any help would be greatly appreciated! Here is a copy of the output after plugging it into visual studios:

Output:
8 6
16 14 2
2 8 14

The relevant fragment is equivalent to the following, with the step-by-step breakdown in comments.

cout << u << " "   // prints 8
     << v          // prints 6
     << endl;      // ends first line "8 6"

int n = two(u, v); // calls 'two' which prints the second line "16 4 2" and returns 2

cout << n          // prints 2 which is the value returned by 'two' at the previous step
     << " " ;      // prints a space but does not end the line

cout << u << " "   // prints 8 next on the third line
     << v          // prints 14
     << endl;      // ends third line and prints "2 8 14"

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