Page 1 of 1

## Recursion Simple introduction into recursion Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=41301&amp;s=1a90911a6bcc078882f4e00407f7388e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 potator

Reputation: 5
• Posts: 84
• Joined: 02-December 07

Posted 19 January 2008 - 11:20 PM

So in the needed tutorial section I saw a couple of requests for recursion. Here's my take.

Recursive methods are essentially methods that call themselves. They require powerful logic skills to comprehend and implement. Lets take the implementation for the Math.power function. We could write it like this:

```public static int power(int base, int power){
if(power == 0)
return 1;
else{
int answer = 1;  int count = 1;
while(count <= power){
count++;
}
}
}

```

This accomplishes the task, but uses a loop. The same code implemented recursively is:

```public static int power(int base, int power){
if (power == 0)
return 1;
else
return base * power(base, power-1);
}

```

As you can see, the latter solution is shortened and, I think, much more elegant. The concept, though, is hard to understand. First, we need to start with a base case, or case where no recursion takes place. In this example, it is power == 0. If we didn't have a base case, the recursion would go on indefinitely. Second, our recursion loop needs to make progress toward the base case or nothing will happen. In this example it is power(base, power-1). Here is a breakdown of what is happening.

Lets take the case of 2 to power of 4. We know the answer is 16, but how does recursion get this.

1) we call power(2,4)
2) power(2,4) calls power(2,3)
3) power(2,3) calls power(2,2)
4) power(2,2) calls power(2,1)
5) power(2,1) calls power(2,0)
6) power(2,0) returns 1
7) power(2,1) returns (2 * 1) or 2
8) power(2,2) returns (2 * 2) or 4
9) power(2,3) returns (2 * 4) or 8
10) power(2,4) returns (2 * 8) or 16

As you can see, power starts at it's highest value and works toward power==0. When it does reach power==0, the recursion moves backward towards the original method call (power(2,4)) a spits out a value. To simply put it, the method expresses 2^4 as 2*2*2*2. The recursion is basically adding another "*2" to the end. This is, in a nutshell, how recursion works.

Is This A Good Question/Topic? 0

## Replies To: Recursion

### #2 warrenbbs

Reputation: 0
• Posts: 2
• Joined: 12-March 08

Posted 12 March 2008 - 01:50 AM

Great post - I've been struggling with the concept! However, what I still don't get is once power gets to 0 and it returns 1, why does it then continue and increment power, and how then does it "know" where to stop and spit out the answer?

I though the "return 1;" line would exit the function..?

Thanks for any help!
Warren

### #3 potator

Reputation: 5
• Posts: 84
• Joined: 02-December 07

Posted 16 March 2008 - 08:41 PM

warrenbbs:

Your question can be explained by tracing the return statements backwards. True, in normal methods, the return statement returns a value to another class, but recursive methods return values to themselves. So in the power(2,4) example, the steps numbered 6 through 9 are returning values to the power method. Step 6 returns the value of 1 to step 5, not the original call of the method. so the line:

return base * power(base, power-1);

in step 5 is now equal to:

return 2 * 1;

but 2 * 1 is not returned to original call of the method, it is returned to the call in step 4. step 4 now returns 2 * 2. This cycle continues until you get back to the very first call of the method, at which the final value is returned to wherever you originally called the method.

### #4 warrenbbs

Reputation: 0
• Posts: 2
• Joined: 12-March 08

Posted 24 March 2008 - 03:27 AM

potator:

thanks - that really helped!

### #5 potator

Reputation: 5
• Posts: 84
• Joined: 02-December 07

Posted 24 March 2008 - 09:13 PM

If you want to see another example of recursion, check out this forum.