Recursion

Steven J. Zeil

Old Dominion University, Dept. of Computer Science

Table of Contents

1. Overview
2. You've Gotta Believe!
3. Recursion versus Iteration
4. Making the Choice

Most of the time, when you want to process a lot of data, you use a loop. Recursion is an alternative that you could employ, but if you're like most programmers, you're a lot more comfortable with looping or iteration than you are with recursion. That can be a problem, in some circumstances, because there are some tasks that really are more easily solved via recursion.

A function is recursive if it calls itself, or calls some other function that eventually causes it to be called again. This means that a recursive function may have several different calls to it active at the same time. In fact, we call the collection of information that represents a function call in progress an activation of the function.

Recursive functions tend to fall into certain familiar patterns. Every recursive function will test its inputs to see if they represent a special case simple enough to be solved without recursion. These are called base cases. When the function's inputs do not constitute a base case, the recursive function must somehow break apart the problem to be solved into one or more smaller sub-problems. These sub-problems are solved via recursive calls, and when the recursive calls are done the function must combine the sub-problem solutions into a solution for the original problem.


In the Forum:

(no threads at this time)