Pass-by-value

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
       Algorithms       Pass-by-value       Gate 2013
Question 1 Explanation: 
Since it is f(p,p)
f(5,5)→f(x,4)*x
f(x,3)*x*x
f(x,2)*x*x*x

⇒x*x*x*x=x4
Now x=x+1, there are 4 different calls namely f(6,4),f(7,3),f(8,2),f(9,1)
Because of pass by reference, x in all earlier functions is replaced with 9.
⇒ 9*9*9*9=94=6561
There is 1 question to complete.
PHP Code Snippets Powered By : XYZScripts.com