Recursion is not a technique that most programmers regularly use. Only a few situations call for recursive programming, and unfortunately, these programs can’t be implemented otherwise, The following sections discuss the dangers of recursion and give you a few hints to help you recognize a procedure that cans for recursive programming ..
It’s Easy to Write a Never-Ending Program
If you forget to specify an exit condition with a few statements that stop the procedure from calling itself, you’ll end up with a never-ending program, or an endless loop. If this happens, your computer will run out of memory for storing the intermediate results and the program will end with the “Out of stack space” error message (see Figure 11.9).The memory available for storing intermediate results. between procedure calls is limited and it’s easy to exhaust.
The stack isn’t used only for recursive procedures ..Each time a function is called; the status of the one that’s interrupted, along with the arguments of the function being called, are stored on the stack. It’s practically impossible to run out of stack pace with.regular procedures: to do so, you’d have to call several hundred procedures, one from within the other. You can run out of stack space with recursive procedures, though, because you don’t have to write several hundred routines only one that calls itself and doesn’t provide,an exit mechanism.
Knowing When to Use Recursive Programming
In addition to knowing how to implement recursive p-ograrnming, you need fa know when to use it, The recursive nature of many problems isn’t obvious, so it may take a while to get the hang of it. (We humans aren’t trained to think recursively, but once you’ve established the recursive nature of a problem, the recursive algorithm will follow quite naturally.) An algorithm that, in the middle of carrying out a task, has to start and complete an identical task is a prime candidate for sive implementation. Consider the Binary Search algorithm, which rejects half theoriginal list and theri has to do the same with the other half. Or consider a prQCedure for scanning the contents of a folder. First, it counts the files. If the folder hai subfolders, the same process must be repeated for each subfolder. If you find yourself nesting loops in many levels or if you’re trying to set up con ditions to exit these loops prematurely, your code would probably benefit from a recursive implementation. Recursion bears some resemblance to iteration, and in many situations, you can implement a recursive algorithm with a loop. The factoial algorithm, for instance, can be easily implemented with a Next loop. But there are situations in which iterations won’t help
Elsewhere in this book you’ll find more examples of recursive procedures. In Chapter 8, there is a recursive procedure for scanning a TreeView control. In Chapter 16, you’ll see how to use recursion to scan all the messages under theInbox folder of Outlook, including subfolders. Now that you have a better understanding of how recursion works, you may wish to take a closer look to these applications.