Recursive Program For Factorial In C
Return 1 is not returning the actual answer. It's just returning the answer to calling factorialfinder(1); which happens in your code. In any program, a call stack is a space in memory that is used to keep track of function calls. Space from this memory is used to store the arguments to a function, as well as the return value of that function.
Whenever some function A calls another function B, A gets the return value of B from that space. A recursive function is nothing special, it's just an ordinary function calling another function (that happens to be itself). So really, when a recursive function F calls itself, it's calling another function: F calls F', which calls F', which calls F', etc.
It's just that F, F', F' etc. Execute the same code, just with different inputs.
The expression if (x 1) is there to check when this process should be stopped. The return value of F' is used by F'.
This C Program prints the factorial of a given number using recursion. A factorial is product of all the number from 1 to the user specified number.
The return value of F' is used by F'. The return value of F' is used by F. In Factorial of some number, the operation is (n). (n-1). (n-2). I've highlighted the 1; this is the condition that's being checked.
A recursive function breaks a big problem down into smaller cases. Going over your program: call factorialfinder with 5, result is stored as 5. factorialfinder(4) call factorialfinder with 4, result is stored as 5.
4. factorialfinder(3) call factorialfinder with 3, result is stored as 5. 4. 3. factorialfinder(2) call factorialfinder with 2, result is stored as 5. 4. 3.
Recursive Program For Factorial
2. factorialfinder(1) call factorialfinder with 1, result is stored as 5.
Factorial In C Programming
4. 3. 2. 1 in essence it combines the result of a stack of calls to factorialfinder until you hit your base case, in this case x = 1.
Pool of radiance ruins of myth drannor full game. • Salminen, Carl (November 24, 2001)... From the original on August 11, 2014. Retrieved July 2, 2012. Archived from on November 26, 2001. Retrieved June 3, 2009.
Factorial In C Language
Since it all happend under the same instance it kept the value of the previous because thats the power of recursion, so pretty much it goes down the chain one by one by calling its own function, why it kept the answer? Im still not sure, im thinking because it all happend with the same variable so when it got to one it still had all the previous calculations in a chain so it got calculated when it hit the case, why did the answer got returned? Lol i still dont get it but im getting familiar with its powers – Aug 6 '13 at 21:20.