Page 1 of 1

Recursion Simple introduction into recursion Rate Topic: -----

#1 potator  Icon User is offline

  • D.I.C Head
  • member icon

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

Post icon  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){
			   answer *= base;
			   count++;
		  }
		  return answer;
	 }
}



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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 potator  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 warrenbbs  Icon User is offline

  • New D.I.C Head

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

Posted 24 March 2008 - 03:27 AM

potator:

thanks - that really helped!
Was This Post Helpful? 0
  • +
  • -

#5 potator  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 xtarx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-April 09

Posted 20 April 2009 - 09:31 AM

thx for this gr8 tutorial it really helped :^: ! . waiting for more ///
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1