943,844 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8760
  • C++ RSS
Aug 15th, 2008
0

Recursive Factorial

Expand Post »
Hi, I am sure you have all seen the recursive factorial program.
C++ Syntax (Toggle Plain Text)
  1. int factorial(int number) {
  2. int temp;
  3.  
  4. if(number <= 1) return 1;
  5.  
  6. temp = number * factorial(number - 1);
  7. return temp;
  8. }


" Once at 1, the values are returned to the previous factorial call and multiplied together."

Could someone explain this to me please, I know it works but when I first read the code I thought it would always return 1 since as I understand it as soon as a function gets to a return value it returns that number (or other type). Since you keep lowering number till you get to 1 I can't see how this doesn't always return 1.

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
coveredinflies is offline Offline
32 posts
since Jul 2008
Aug 15th, 2008
0

Re: Recursive Factorial

Maybe it would be easier if you thought about the code like so:

C++ Syntax (Toggle Plain Text)
  1. int factorial ( int number )
  2. {
  3. int temp;
  4.  
  5. if ( number <= 1 )
  6. {
  7. return 1;
  8. }
  9. else
  10. {
  11. temp = number * factorial ( number - 1 );
  12. return temp;
  13. }
  14. }
Last edited by iamthwee; Aug 15th, 2008 at 8:35 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 15th, 2008
0

Re: Recursive Factorial

Don't worry, simply form a habit to read (then invent) recursive algorithmes (apropos, never calculate factorial in this manner).
How about:
C++ Syntax (Toggle Plain Text)
  1. int factorial(int n)
  2. {
  3. return n <= 1? 1: n*factorial(n-1);
  4. }
Simple and clear code (with guarantee of as usually undetected integer overflow)...
Last edited by ArkM; Aug 15th, 2008 at 11:30 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 15th, 2008
0

Re: Recursive Factorial

why don't u trace for a number like 4?
of course the code u've come up with 'is' actually more complicated than a simple code for a factorial should be! but tracing it will help

C++ Syntax (Toggle Plain Text)
  1. int factorial(int number) {
  2. int temp;
  3.  
  4. if(number <= 1) return 1;
  5.  
  6. temp = number * factorial(number - 1);
  7. return temp;
  8. }
  9. int factorial(int number) {
  10. int temp;
  11.  
  12. if(number <= 1) return 1;
  13.  
  14. temp = number * factorial(number - 1);
  15. return temp;
  16. }
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Aug 15th, 2008
0

Re: Recursive Factorial

Yet another productive method: try to "unwind" solution from the END of recursion.
For example, in factorial case:

When the function knows the answer? Of course, if n <= 1 - factorial is equal to 1 by definition! OK, write the first statement:
C++ Syntax (Toggle Plain Text)
  1. int factorial(int n)
  2. {
  3. if (n <= 1)
  4. return 1;
  5. // But what else?...
  6. }
Let's continue. Suppose we HAVE a function to calculate factorial for all small numbers < n.
C++ Syntax (Toggle Plain Text)
  1. in factorial(int n)
  2. {
  3. if (n < 1)
  4. return 1; // OK, it's our point of rest...
  5. // We are on terra incognita now, but
  6. // let's remember school math...
  7. // Factorial definition helps:
  8. return factorial(n-1) * n;
  9. }
Stop. See how the land lies.

We are trying to call factorial for lesser than n number then more lesser and so on.
We are going to the bottom - of what? Where is this bottom?

Aha, come back to the 1st and 2nd line of the function body. That's a bottom! After that we will rise to the surface, back to starting recursion level, and - hurrah - we know factorial(n-1) - that's all, all done.

It looks like a trick or abstraction: we have no function yet but operate with its call(s) to build its body...

But it's the power of recursive solutions.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 16th, 2008
0

Re: Recursive Factorial

with the recursive functions you should always have a stop point somewhere in ur function otherwise it will go on forever. Your stop point in this case is the "return 1" which means that you can only exit the program when you have reached the number 1 (because u've got the (n<=1) condition)---it is ur only way to end the recursion
'return 1' here means in fact 'return true'....try replacing it ...it still works
bcuz you are not actually returning the 'number' 1 but you are telling your compiler that it is now ok to exit the program....this is where i want my code to end
but do u kno why if we put 'return 0' instead of 'return 1', the output will be zero?
'return 0' can actually be equivalent to 'return false', so the recusion ends but a false end
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Aug 16th, 2008
0

Re: Recursive Factorial

Hey thanks for the replies, I totally wasn't getting it and didn't understand your first few posts then I was reading fifth post and it clicked.

the last return 1 returns to the previous function call not to the main function. thank you again I was getting really frustrated. so obvious in hindsight!
Reputation Points: 10
Solved Threads: 0
Light Poster
coveredinflies is offline Offline
32 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Char* to int or int*
Next Thread in C++ Forum Timeline: pls help palindrome





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC