Presentation is loading. Please wait.

Presentation is loading. Please wait.

10.3 Details of Recursion.

Similar presentations


Presentation on theme: "10.3 Details of Recursion."— Presentation transcript:

1 10.3 Details of Recursion

2 Recursion vs. Iteration
Some problems which can be solved using recursion are rather complex. We are only addressing relatively straightforward examples here. You might observe that the algorithm for finding the number of balls in a pool rack could be implemented using a loop rather than recursion. The fragment below illustrates how simple this can be: int sum = 0; for(int i = 1; i < n; i++) sum = sum + i;

3 Recursion vs. Iteration, cont.
This is simpler than recursion, and in general, any of the examples given here using recursion could be implemented using loops. However, you need to know about recursion for at least two reasons: 1) Because it is syntactically possible to call a method within itself, even if you never intend to do so, you need to know what happens in case you do so accidentally. In other words, the existence of this capability in the syntax leads to the possibility of new logic errors which you need to be aware of.

4 Recursion vs. Iteration, cont.
2) Some more advanced concepts in computer science are more simply explained in terms of recursion rather than loops. For this reason you should have some idea of this abstraction even if you never plan on using it in your own programs.

5 Infinite Recursion Once you learned how to program using loops, you found out there was a special kind of problem that they were prone to: an infinite loop. Likewise, once you learn how to program using recursion, you have the possibility of a new kind of error: infinite recursion. This kind of error is quite unpleasant. Infinite recursion can cause the process or system to lock up, making it necessary to restart in order to continue.

6 Why Does Infinite Recursion Happen?
The answer is that each call to a method takes a certain amount of space in system memory to hold copies of the parameters, local variables, and other things that belong to the call. When infinite recursion happens, all free memory can eventually be taken up by the calls, until no memory is left. When the process or system no longer has any free memory to work with, it locks up. Debugging recursion requires careful reading of the code because the compiler may not detect problems and run time problems can be so intractable.

7 Incorrect Implementations of Recursion
There are many ways to make mistakes and many unusual results that might come from incorrect recursive code, but the most important and typical kinds of mistake have to do with the base case. Either the body of the method is not structured as an if with two alternatives, one of them the base case; or the test condition of the base case is not correctly written; or the sequence of values sent in successive calls to the method do not approach and ultimately reach the value tested in the base case condition. These defects can lead to infinite recursion and other problems.


Download ppt "10.3 Details of Recursion."

Similar presentations


Ads by Google