• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion Help  RSS feed

 
Michael Patrick
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

------------------------------------------------------------------------
i cant understand how it prints 1, 2, 4, 9

the way i see it is that once it is called with homer(9) the if evaluates to false so the else is executed and homer is called with 4 and the println is skipped. again the if is false so homer is called with 2 (println is skipped) and then 1 so only 1 should be printed out and control should go back main.

can someone please write a step-by-step explanation as to what is happening. the authors explanation is horrible for a beginner.
[ May 01, 2005: Message edited by: Michael Patrick ]
 
Amirthalingam Prasanna
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
homer(9)->homer(4)->homer(2)->homer(1)
This is the flow and now the homer(1) is executed which will print 1 but still the n value is unique to each function calls and the print for up the function call chain will remain. so next its print for 2 and then 4 and finally 9. It will go in reverse of the function call and finally will return to the main method.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a neat example. The method calls itself before it displays the current value. So when the value is 9 it calls itself before displaying 9. When it calls itself the value is 4 and it calls itself again before displaying 4. And so on. On the way back out it displays the current value after calling itself so 4 displays before 9.

Reverse these two lines and see what happens:

Keep these examples somewhere. One day you're going to want to reverse a string or do some other recursive task that seems to put out the results backwards and it will be good to have this template "in your pocket".
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Patrick:
the way i see it is that once it is called with homer(9) the if evaluates to false so the else is executed and homer is called with 4 and the println is skipped. again the if is false so homer is called with 2 (println is skipped) and then 1 so only 1 should be printed out and control should go back main. ...
[ May 01, 2005: Message edited by: Michael Patrick ]

You are right on track with your explanation here except for the very last part. When the call homer(1) finishes, the program does not jump straight back to main(). Remember that when any function call returns, execution picks back up with the line immediately after the function call. This means that after the program prints "1", it returns back where homer() was called with 2, so it prints out "2" next. Then it returns to where homer() was called with 4 and prints "4". Then it returns to where homer() was called with 9 and prints "9". THEN it returns to main().

HTH

Layne
[ May 02, 2005: Message edited by: Layne Lund ]
 
Michael Patrick
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote some debugging comments that will explain the whole process. go through it step-by-step and it will make sense.



[ May 02, 2005: Message edited by: Michael Patrick ]
[ May 02, 2005: Message edited by: Michael Patrick ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Patrick:
i wrote some debugging comments that will explain the whole process. go through it step-by-step and it will make sense...


That's a GREAT way to see what a program is doing!

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!