The basic concepts and ideas involved with recursion are simple: a function that has to solve a big problem uses itself to solve a slightly smaller problem. Understanding the basic idea is fairly straightforward. However, to truly understand the intricacies of recursion, and to be able to use it well in one's own programs, requires a lot of practice. The best way to obtain this practice is to write a lot of recursive functions. In this section we'll do just that.
We begin with the string library. The section will cover both iterative and recursive implementations of many string library functions, showing the similarities and the differences in the approaches taken. Note that if you were to implement most of these functions for real world use, you would not do them recursively for efficiency reasons. However, they provide good ground for practice.
Next we'll look at how recursion can be used in searching and in sorting to increase the efficiency of these operations. Then we'll look at how recursion can be used for certain mathematical problems, such as printing a number in different bases and computing different sequences of numbers. For most of these problems, recursion presents an incredibly elegant solution that is easy to code and simple to understand.
We will then explore how recursive functions make using inherently recursive structures, such as trees, much easier. If you are unfamiliar with trees, please see the SparkNote on the topic.
Lastly, we will look at how recursion can be used to solve well-known problems, such as The Towers of Hanoi.
You should go through the examples in this section thoroughly. Study each example for as long as you need to understand it fully. Many examples are presented, and if you can come away with a complete understanding what each one is doing, you will have a firm grasp on how recursion works.
Take a Study Break!