Arjun Suresh (talk | contribs)  (Created page with "Q.42 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 param...")  | 
				Arjun Suresh (talk | contribs)   | 
				||
| Line 1: | Line 1: | ||
Q.42 What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that    | Q.42 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.    | the first parameter is passed by reference, whereas the second parameter is passed by value.    | ||
| − | + |   <syntaxhighlight lang="c">  | |
int f (int &x, int c) {    | int f (int &x, int c) {    | ||
  c = c - 1;    |   c = c - 1;    | ||
| Line 8: | Line 8: | ||
  return f(x,c) * x;    |   return f(x,c) * x;    | ||
}    | }    | ||
| − | + | </syntaxhighlight >  | |
(A) 3024    | (A) 3024    | ||
| Line 18: | Line 18: | ||
===Solution===  | ===Solution===  | ||
| + | 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. The correct code must replace  | ||
| + |  return f(x,c) * x;   | ||
| + | with  | ||
| + |  res = f(x,c);  | ||
| + |  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, because of pass by reference, in all the previous functions, x is now 9. Hence, the value returned by f(p,p) will be 9 * 9 * 9 * 9 * 1 = 6561.  | ||
| + | |||
| + | {{Template:FBD}}  | ||
| + | |||
| + | |||
| + | [[Category: GATE2013]]  | ||
| + | [[Category: Previous year GATE questions]]  | ||
| + | [[Category:Coding Questions]]  | ||
Q.42 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.
<syntaxhighlight lang="c">
int f (int &x, int c) {
c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x;
} </syntaxhighlight > (A) 3024
(B) 6561
(C) 55440
(D) 161051
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. The correct code must replace
return f(x,c) * x;
with
res = f(x,c); 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, because of pass by reference, in all the previous functions, x is now 9. Hence, the value returned by f(p,p) will be 9 * 9 * 9 * 9 * 1 = 6561.
Q.42 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