# Examples of Recursion

### Introduction and Summary

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

### Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

### Are you afraid of relationships?

Auntie SparkNotes can help!

### Sexy starlet style

See every single look from the Met Gala!

### Geeky Actors: Then and Now

Travel back in time!

### 10 Movies Better Than Their Books

What do you think?

### How To Look Like J-Law...

When you don't look like J-Law.

### 12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

### Villains We Want These Actresses to Play

From super cute to super bad!

## The Book

### Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps