Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
int reset(int x) {
x = 10;
return x;
}
int main() {
int n = 1;
int m = reset(n);
std::cout << n << ' ' << m;
}1 10
- Expecting
10 10:xis a copy ofn, so assigning toxleavesnunchanged. - Expecting
1 1: a function may change its own copy, andreturn xreturns the changed value.
- How would you change
resetso that the call also setsnto10? - When is passing a parameter by
constreference better than passing it by value?