The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+34 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;
asked in Programming by Boss (18.2k points)
recategorized by | 5k views
@Arjun Sir.. If the first parameter were passed by address (using pointers), will it still be undefined behaviour??

Apart from what arjun sir explained, rest is not typing mistake or error. 


Reference :

1 Answer

+54 votes
Best answer

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

answered by Veteran (400k points)
edited by

Sir here is the question I am taking about.

I have uploaded a picture in the comments about the solution. Can you please look into it?


int f (int &x, int c)
it should be * not &. I may be wrong. But i never see such thing. So Give me example where i can use this.

x = x + 1; // This is also wrong.
It should be.
*x = *x + 1.

" But due to pass by reference, x in all the previous functions is now 9 "

Why this happened ? Can Somone explain plz?

@Pawan Kumar i.e how call by reference work,updated the value hand to hand.
Yes, there are many typing mistakes in the question.

Its not a mistake bro.. This concept is called aliasing which is used in C++ . Here you can directly use "x" without any de-referencing operator. Its like a extra name(nick name) to the same variable . 


Does this mean that int &x=p is a way to declare x as a pointer to p?

Also, would x=x+1 mean p=p+1?


Code Implementation

Beware, it's only after the problem given in the code has been addressed as suggested by arjun sir. This is not the real program as given in the question.


This is the code for above question:

#include <stdio.h>

int p=5;

int f (int *x, int c) {
c = c - 1;
if (c==0) return 1;
*x = *x + 1;
int res=f(x,c);
return  res*(*x);


int main()
    return 0;


OUTPUT: 6561


Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,443 questions
53,648 answers
70,909 users