• recategorized by
33,998 views
104 104 votes

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;
}

4 Answers

Best answer
156 156 votes

In GATE 2013 marks were given to all as the same code in C/C++ produces undefined behavior. This is because $*$ is not a sequence point in C/C++. The correct code must replace:

return f(x,c) * x;
with
res = f(x,c); // ';' forms a sequence point 
//and all side-effects are guaranteed to be completed here 
//-- updation of the x parameter inside f is guaranteed 
//to be reflected in the caller from the next point onwards. 
return res * x;

In this code, there will be 4 recursive calls with parameters $(6,4), (7,3), (8,2)$ and $(9,1)$. The last call returns $1$. But due to pass by reference, $x$ in all the previous functions is now $9$. Hence, the value returned by $f(p,p)$ will be $9 * 9 * 9 * 9 * 1 = 6561$.

Good Read:

• edited by
4 4 votes
I think Answer is (B)
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 becomes9 after f(x, 2) call. So the value of expression x^4 becomes 9^4 which is 6561
1 1 vote

Call: f(5, 5)

  • The second argument (c) counts down.

  • The first argument (x) increases each step.

The function stops when c becomes 0.

Each step does:

  1. Increase x by 1

  2. Decrease c by 1

  3. Multiply the result of the next call by the new x

Call x starts asc starts asAfter x++After c--Result 
f(5,5)      5                    5                    6                4                f(6,4) * 6
f(6,4)6473f(7,3) * 7
f(7,3)7382f(8,2) * 8
f(8,2)8291f(9,1) * 9
f(9,1)91stop0return 1

 

Solve in Reverse order

f(9,1) = 1
f(8,2) = 1 × 9 = 9
f(7,3) = 9 × 8 = 72
f(6,4) = 72 × 7 = 504
f(5,5) = 504 × 6 = 3024

Return value = 3024

 

Answer:
Position:
Show:

Related questions

73 73 votes
3 answers 3 answers
19.7k
19.7k views
gatecse asked Aug 21, 2014
19,671 views
Which one of the following is NOT logically equivalent to $¬∃x(∀ y (α)∧∀z(β ))$ ?$∀ x(∃ z(¬β )→∀ y(α))$$∀x(∀ z(β )→∃ y(¬α))$$∀x(∀ y(α)→∃z(¬β ))$$∀x(∃ y(¬α)→∃z(¬β ))$
49 49 votes
5 answers 5 answers
17.2k
17.2k views
Kathleen asked Sep 17, 2014
17,230 views
The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.global int i=100, j=5...
27 27 votes
3 answers 3 answers
7.2k
7.2k views
go_editor asked Apr 24, 2016
7,186 views
For the following code, indicate the output if static scope rulesdynamic scope rulesare usedvar a,b : integer; procedure P; a := 5; b := 10; end {P}; procedure Q; var a, ...
54 54 votes
5 answers 5 answers
22.3k
22.3k views
go_editor asked Apr 24, 2016
22,296 views
The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.global int i=100, j=5...