C++ | References | Question 1
What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f(int &x, int c) {
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}
(A) 3024
(B) 6561
(C) 55440
(D) 161051
Answer: (B)
Explanation: Since c is passed by value and x is passed by reference, all functions will have same copy of x, but different copies of c.
f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4
Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the value of expression x^4 becomes 9^4 which is 6561.
#include <stdio.h> int f(int &x;, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; } int main() { int p = 5; printf("%d", f(p, p)); } |
chevron_right
filter_none
Recommended Posts:
- C++ | References | Question 6
- C++ | References | Question 5
- C++ | References | Question 6
- C++ | References | Question 6
- C++ | References | Question 4
- References in C++
- Pointers vs References in C++
- Can references refer to invalid location in C++?
- Default Assignment Operator and References
- C++ | Templates | Question 5
- C++ | Misc C++ | Question 6
- C++ | Inheritance | Question 14
- C++ | Misc C++ | Question 7
- C++ | Misc C++ | Question 8
- C++ | Destructors | Question 5



