[英]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!
但是函数二返回了 cout 而不是返回(b),谁能告诉我我做错了什么,任何帮助将不胜感激! Here is a copy of the output after plugging it into visual studios:
这是将其插入 Visual Studios 后的输出副本:
Output:输出:
8 6 8 6
16 14 2 16 14 2
2 8 14 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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.