## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

Chapter 4

# Using Recursion

In This Chapter

Introducing recursion

Calculating factors with recursion

Listing directories with recursion

Sorting with recursion

Recursion is a basic programming technique in which a method calls itself to solve some problem. A method that uses this technique is called recursive. Many programming problems can be solved only by recursion, and some problems that can be solved by other techniques are better solved by recursion.

I'm not sure, but I think that the term recursion comes from the Latin recurse, recurset, recursum, which means to curse repeatedly. I do know that that's exactly what many programmers feel like doing when they're struggling with complex recursive programming problems.

True, the concept of recursion can get a little tricky. Many programmers steer clear of it, looking for other techniques to solve the problem at hand, and in many cases, a nonrecursive solution is best. Many problems just cry out for recursion, however.

## Calculating the Classic Factorial Example

One of the classic problems for introducing recursion is calculating the ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required