0

Why do we use return 1 to terminate the recursive function? Can any other value be used as a default value like 1.

And if we return 1 as return value of the function, then why 1 is not returned to main function.

 #include<stdio.h>
 int fact(int n)
 {
   if(n>=1)
      return (n*fact(n-1));
   else
      return 1;
 }
 int main()
 {
   int a,ans;
   scanf("%d",&a);
   ans=fact(a);
   printf("factorial of %d is %d ",a,ans);
   return 0;
  }
  /*
   explanation
          fact(4);
          if(4>=1) 4*fact(3)
          if(3>=1) 4*3*fact(2)
          if(2>=1) 4*3*2*fact(1)
          if(1>=1) 4*3*2*1*fact(0)
          if(0>=1) return 1;

  */
3
  • 3
    Because 0! is 1. Commented May 21, 2019 at 8:30
  • 3
    Your comment is the answer. You are calling fact(4), not fact(0) Commented May 21, 2019 at 8:30
  • 1
    please only tag the language you are using. Your question is rather unclear. Try fact(0) and use a debugger to see that 1 is indeed returned to main Commented May 21, 2019 at 8:30

4 Answers 4

3
int fact(int n)
{
    if (n >= 1)
        return n * fact(n-1);
    else
        return 1;
}

Each time the fact() function is called, it runs either the return n * fact(n-1); statement OR the return 1; statement but not both.

You call fact(4) in main(). This is how it runs:

main:
  compute fact(4)
  fact(4):
  |  4 >= 1?  Yes!
  |  compute 4 * fact(3)
  |    fact(3):
  |    |  3 >= 1?  Yes!
  |    |  compute 3 * fact(2)
  |    |    fact(2):
  |    |    |  2 >= 1? Yes!
  |    |    |  compute 2 * fact(1)
  |    |    |    fact(1):
  |    |    |    |  1 >= 1? Yes!
  |    |    |    |  compute 1 * fact(0)
  |    |    |    |    fact(0):
  |    |    |    |    |  0 >= 1? NO!
  |    |    |    |    |  return 1;
  |    |    |    |    +--> 1
  |    |    |    |  fact(0) is 1, return 1 * 1 (--> 1)
  |    |    |    +--> 1
  |    |    |  fact(1) is 1, return 2 * 1 (--> 2)
  |    |    +--> 2
  |    |  fact(2) is 2, return 3 * 2 (--> 6)
  |    +--> 6
  |  fact(5) is 6, return 4 * 6 (--> 24)
  +--> 24
  fact(4) is 24, assign it to `ans`, print it etc
// end of main

When a function uses the return statement (or after it executes its last statement if a return is not reached, the control is passed back to the expression that has called it.

Sign up to request clarification or add additional context in comments.

Comments

3

why do we use "return 1" to terminate the recursive function

Because this is supposed to cover the case for when n is not >=1, in other words when n is 0. I don't think negative n are valid. 0! is 1, hence why it returns that value.

And if we return 1 as end of function, then why 1 is not returned to main function.

If the function is called with 0 or 1 for n, then 1 is returned to the main function. For any other value, 1 is only returned in the recursive factorial calls and the value which is returned to the main function isn't 1, but (n*fact(n-1)), which isn't 1 in those cases.

2 Comments

then why 1 is not returned to main function... I think that's a bit different from what you explained in the last paragraph.
Good catch. I'll fix it.
2

The return statement is executed when n==0.

The factorial for n==0 is 1, so we return 1.

Comments

0
   /*
      explanation
          fact(4);
          if(4>=1) 4*fact(3)
          if(3>=1) 4*3*fact(2)
          if(2>=1) 4*3*2*fact(1)
          if(1>=1) 4*3*2*1*fact(0)
          if(0>=1) return 1; now return is default tend to multiply as we give 1 and 
           return has already 24 in its stack so 1*24 is returned to main()
          if we give return 2; 2*24 is returned to main();

    */

we don't want our final result to be affected, to solve that error, anything multiple by 1 will result the same, so we use 1 as return in recursive function.

Actually return is also a stack register which holds temporary variable while calling with in the function, and it operates default by multiply property, as we can send only one return value always.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.