difference between int f(int &x) int f(int x)

#include<stdio.h> #include<conio.h> int f(int & ,int );//function prototype main() { int x,p=5; //p is initialized to 5 x=f(p,p); printf("\n Value is : %d",x);//print the value of x getch(); } int f (int & x, int c) { c=c-1; if (c==0) return 1; x=x+1; return f(x,c) * x; //recursion } 

output : 6561

can anyone explain me the flow the program This question is from gate i couldn't understand it. It seems that the function is called with value of p = 5. It is catched in the function f by int &x the problem is here. Is the value i.e 5 is stored in x or in address of x.

3

6 Answers

This code uses a C++ reference, which is what the int & syntax means.

A reference is, basically, syntactic sugar for a pointer. So when you call f(p, p), the function argument x is a reference to p in main(), while c is merely a copy of the value at the time of the call.

This means f can change the value of p in main(), through the reference. Since f calls itself recursively, passing the same reference toitself, it's always a reference to the p in main().

To track the recusion, I would suggest adding logging print-outs at strategic places inside f().

The first creates a reference which is more like a cleaner syntax instead of a pointer.

You can use it to change contents of variables among calls to function.

Although you must note, the code you have posted is not valid C++. C++ does not allow main without a return type.

More simple:

int f(int &a){ a = 5; } int x = 0; f(x); //now x equals 5 int f2(int b){ b = 5; } int y = 0; f2(y); //y still equals 0 
0

Actaully it is like this the reference part in the question was to distract u it is just x pointed to the address of the the p but the 'x' itself contained the value 5 at the first time but the recurrsion ran for 4 times so in the stack it was like ----[x when value 6(bottom of the stack), xwhen value 7, xwhen value was 8 , x when value is 9(top of the stack)] ...at the top it was 9 but as we know that in recurrsion first loop execution takes place and then the evaluation so when the value of x changed to 9 then all the four block of the stack changed to 9 since they pointed to the same 'x'....so it came out to be 9*9*9*9=6561 ..as simple as that.

  • & is reference: it means it must be an object's reference (address);
  • * is a pointer: it points to a object.

The difference is that "&x" can not be "NULL", but &x can be NULL.

These two prototype are different.

int f(int & ,int ); int f(int * ,int ); //are different function prototypes 

This is a good question.

The code is calculating (2P-1)^(P-1) Try various values for P to find the pattern. Here, P=5; so it yields 9^4 = 6561.

پویا پاکاریان

پویاپاکاریان

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like