Thread: Cuckoo Clock Recursion View Single Post
 Recursion
 chickentree Super Moderator   Status: Offline Posts: 250 Join Date: Dec 2012 Location: Frosno, Ca Recursion - 08-18-2014, 12:25 PM Remember that recursion is expensive in terms of memory use. Why? Because the first call is open (taking up memory for variables and other house keeping) when it calls itself. The second call has take up memory etc when it calls itself and this continues until the recursive function either runs out of memory (blows up) or a condition is met which, instead of having the method call itself again, cause the method to return to its caller which follows the stack of opened methods until the program returns to the first call and finally ends. Also keep in mind that recursive methods can perform actions either before calling themselves, when returning or after returning. This becomes very important as you learn more about programming later on. So the question becomes what do you recurse on? In your case you have basically four things that you are manipulating: The pendulum, the hour hand, the minute hand and the cuckoo. But due to the wording of the problem, two of these items do not depend on the clock’s state changing, in other words they are independent of the clock, and two are dependant, the do depend on the clock being in a certain configuration. By “state” I mean anything to do with the clock, its subparts or variables. One of these independent variables is the pendulum. In this case the pendulum should swing back and forth continuously. So using recursion on the pendulum would probably result in Alice dyeing in a disastrous way. At this point I think I might have said too much on the subject of what to recurse on. But remember that the recursive call can return a value to be used in further calculations. So basically a recursive method might look something like this: Code: ```int recurse parameter int result do something before recursion // if needed if you are not done //test if end criterion is met result = recurse result // recurse: new result = recurse old result. endif do something after recursion // if needed return result``` To forget recursion you must first forget recursion! Hopefully this is enough to get you started Mark Mark Henwood mhenwood@ieee.org Last edited by chickentree; 08-18-2014 at 12:28 PM.