Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,491 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,192 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Recursion

 
Reply to this topicStart new topic

> Recursion, Simple introduction into recursion

potator
Group Icon



post 19 Jan, 2008 - 11:20 PM
Post #1


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:

CODE

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


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

CODE

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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

warrenbbs
*



post 12 Mar, 2008 - 01:50 AM
Post #2
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
Go to the top of the page
+Quote Post

potator
Group Icon



post 16 Mar, 2008 - 08:41 PM
Post #3
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.
Go to the top of the page
+Quote Post

warrenbbs
*



post 24 Mar, 2008 - 03:27 AM
Post #4
potator:

thanks - that really helped!
Go to the top of the page
+Quote Post

potator
Group Icon



post 24 Mar, 2008 - 09:13 PM
Post #5
If you want to see another example of recursion, check out this forum.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 9/7/08 02:12PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month