![]() |
Understanding Recursion
I'm trying to understand the classic compute the factorial of a number business using recursion. Let's say I want to compute the factorial of 4 so in my main function I have something like this:
:
int x = 4;Then my function definition goes like this: :
int factorial(int number)I guess the way I got it figured so far is this. The value of x which is 4 gets passed to number. Then we have a succession of function calls like this: :
return 4 * factorial(3)And I think then that would be all. But it's hard to see the intermediate results with all these successive function calls. One of my books does recommend putting in a statement that would print the level at which each call is and what the intermediate result is. In order for me to do that, would I just do this? :
elseLet me know what the best way to understand this is. |
Follow your books suggestion, so you can see the logic in the recursion.
:
int factorial(int number)The flow will be: 4 * factorial(4 - 1) 3 * factorial(3 - 1) 2 * factorial(2 - 1) Answer: 24 Mathematically the factorial of 4, would equate to: 4 × 3 × 2 × 1 = 24 Do you see the pattern? |
Following with a debugger can also be useful.
|
All I see is a succession of function calls, over and over. What I don't see is where the actual calculations are carried out. The return statement does not say return 4 * 3, or return 3 * 2, or return 2 * 1, or whatever. It actually says return 4 * factorial(3). Then the function calls itself again, and it says return 3 * factorial(2). Then the function calls itself again, and it says return 2 * factorial(1). I realize that somehow it ends up as 4 * 3 * 2 * 1, but the code doesn't really look like that to me. How are the actual calculations carried out? All I see is just the function calling itself until we get down to where the value in number is 1, meaning the function terminates.
|
Quote:
I'm going to try to explain. Say you have the following code: :
#include <iostream>And if you were to run it: :
Hello! The program is in cube()As you can see, in order to figure out the cube of a number, first the square must be found. The cube() function won't be able to return a result until the square() function is complete because the result of square() is needed. When the square function finishes and returns its result, we are returned to the scope of the cube() function which then processes the result and returns its own. Makes sense? Recursion is exactly the same, except that instead of calling another function, the same function is called again. For example, consider that n is equal to 2. :
return n * factorial( n - 1);In this example, the factorial function is called with an argument of 1. That call results in a 1 (1! = 1), and is multiplied by 2 to produce 2. Recursion is just a chain of calls. The chain only ends when the last call completes. At that point, the calculated value trickles upward and is returned as a final result. For example, here is the evaluation of factorial(4): :
4 * factorial( 3 )I hope that helped. :) |
Quote:
If you were to roll it out, it might look something like this (psuedocode) :
{Since the function calls only provide indirection, and no actual calculation, the above "unrolling" ends up looking like this :
{ |
Quote:
Any recursive function needs a condition that backs it out, eventually, to the original call. |
Quote:
|
Some folks... ya just can't reach.
|
Difficult to see. Very. Since you really can't see the calculations. All I see is the chain of function calls. And someone posted:
:
4 * factorial( 3 )4 * 3 is not the same as 4 * factorial(3). I see the compiler always calling the function but I don't see the 4 * 3 * 2 * 1. |
| All times are GMT -5. The time now is 3:05 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC