-4

Why the output of the above code is 5 as the function should go to first 5-4-3-2 (since no output) loop decrements to 1 so return 12 so at last 12+1..... so the answer must not be 5 i guess ?

using namespace std;
int  x()
{
}
int reee(int n)
{
  for(int i=n; i>0; i--)
  {
  if(i==2)
  {
    return x();
  }
  else if(n==1)
  {
    return 12;
  }
  else
  {
     return reee(i-1)+1;
  }
}
}
int main()
{
  cout << reee(5) << " ";
}
Benjam
  • 1,401
  • 2
  • 16
  • 22
  • 1
    This does not compile. Also, what is `return x();` supposed to do? – NotAProgrammer Jul 03 '20 at 12:47
  • 2
    Wht is `x()` empty? – Shivam Jha Jul 03 '20 at 12:48
  • 8
    Your program has undefined behaviour since `x` does not return anything. – molbdnilo Jul 03 '20 at 12:48
  • it will never get to n==1, because at i==2, x() is called, which is non-recursive. – RL-S Jul 03 '20 at 12:49
  • Note that your loop always returns on the first iteration, so it will never loop. – molbdnilo Jul 03 '20 at 12:49
  • I think you have a serious misunderstanding here `since no o/p`. `x` promises to return a value but doesn't. That means you program has undefined behaviour. – john Jul 03 '20 at 12:51
  • 2
    did you write this code? In any case I would suggest to start from scratch. It is not clear what it is supposed to do, but it is clear that it doesn't do that. This looks like a good opportunity to learn how to use a debugger – 463035818_is_not_an_ai Jul 03 '20 at 12:54
  • "_Why the final output of the recursive function is 5?_" - "_so the answer must not be 5 i guess ?_" - No that's not how this works. Add your debugging efforts into it. Where does it go wrong? Explain how you came up with this and what you expected it you return. – Ted Lyngmo Jul 03 '20 at 18:04
  • Please indent the code, it is unreadable, and that's how bugs are introduced..... – Yunfei Chen Jul 03 '20 at 20:48

2 Answers2

1

There is no need for a for loop in this case as your function is already a recursive function so it keeps running the function until it is 2. When it is 2, it returns x making it empty body + 3. If you want it to get to 1, in the if statement of i==2, you should make it return(i-1) Therefore, your code should be like this:

using namespace std;

int reee(int n)
{
  if(n==2) {
    return reee(--n);
  } else if(n==1) {
    return (n+12);
  } else {
     return reee(--n)+1;
  }
}
int main()
{
  cout << reee(5) << " ";
}

That should help you with what you are trying to achieve.

scoochy
  • 693
  • 1
  • 6
  • 14
  • Actually my doubt was not with the logic of the code. I actually wanted to know why the output is 5 of my code. what is happening there? – Kaustav Bora Jul 04 '20 at 11:21
  • Your for loop runs only once or doesn't even run at all, and since the variable i is set to n which is 5, it returns (5-1)+1 which will give you 5 at the end and that is why the output of your code is 5. – scoochy Jul 04 '20 at 16:15
  • But for the first time lets suppose n=5 then it return ree(4) and then the recursion goes on.The recursion takes place here if I am not wrong !! so why the final output is 5 – Kaustav Bora Jul 05 '20 at 17:52
0
int  x()
{
}

Calling this function results in Undefined Behavior because your function declared to return int doesn't return anything.

Crank up the compiler warning and while you are in learning stages treat warnings as errors.

bolov
  • 72,283
  • 15
  • 145
  • 224