| | |
Recursive Factorial
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2008
Posts: 32
Reputation:
Solved Threads: 0
Hi, I am sure you have all seen the recursive factorial program.
" 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.
C++ Syntax (Toggle Plain Text)
int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp; }
" 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.
Maybe it would be easier if you thought about the code like so:
C++ Syntax (Toggle Plain Text)
int factorial ( int number ) { int temp; if ( number <= 1 ) { return 1; } else { temp = number * factorial ( number - 1 ); return temp; } }
Last edited by iamthwee; Aug 15th, 2008 at 8:35 am.
*Voted best profile in the world*
Don't worry, simply form a habit to read (then invent) recursive algorithmes (apropos, never calculate factorial in this manner
).
How about:
Simple and clear code (with guarantee of as usually undetected integer overflow
)...
).How about:
C++ Syntax (Toggle Plain Text)
int factorial(int n) { return n <= 1? 1: n*factorial(n-1); }
)... Last edited by ArkM; Aug 15th, 2008 at 11:30 am.
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
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
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)
int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp; } int factorial(int number) { int temp; if(number <= 1) return 1; temp = number * factorial(number - 1); return temp; }
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:
Let's continue. Suppose we HAVE a function to calculate factorial for all small numbers < n.
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.
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)
int factorial(int n) { if (n <= 1) return 1; // But what else?... }
C++ Syntax (Toggle Plain Text)
in factorial(int n) { if (n < 1) return 1; // OK, it's our point of rest... // We are on terra incognita now, but // let's remember school math... // Factorial definition helps: return factorial(n-1) * n; }
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.
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
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
'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
•
•
Join Date: Jul 2008
Posts: 32
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- Order of a recursive function (C)
- printing recursive call parameter (C++)
- Recursive Insertion on Linked Lists (C++)
- need Recursion help (C)
- Factorals. (C++)
- Recursive functions??? (C)
- Factorial program (Java)
Other Threads in the C++ Forum
- Previous Thread: Char* to int or int*
- Next Thread: pls help palindrome
Views: 3225 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int java lib library lines linkedlist linker loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






