This preview shows pages 1–3. Sign up to view the full content.
Notes on Recursive Functions
Last modified: 8/5/08
Preface
This page is a "howto" on setting up recursive functions for introductory and intermediate programming
courses. There are a lot of other applications and there's plenty of theory out there, but I'm not going to go
into that. Here's one particularly good presentation that defines recursion; I shall not attempt to recreate
what's already been done well  I'll just
link
to it instead. My goal here is to familiarize you with the basics
of how to create and use recursive functions.
Parts of Recursive Definitions
Before we write a recursive function, it's more or less essential to write a recursive definition (or do some
form of prewriting). Recursive functions work based on the values of their parameters. For the sake of
simplicity to start out, we'll look at recursive functions with one parameter,
n
, assumed to be an integer.
ALL
recursive definitions are broken up into two parts:
Base
Values of
n:
usually (not always)
n
= 0 and/or
n
= 1.
Idea:
For these values of
n
, the function is defined simply. The function returns a preset value, e.g. 0, 1, the
value of one of the other parameters, etc.
Examples:
•
f(0) == 0 and f(1) == 1
•
f(0) == parameter2
Recursive
Values of
n
:
usually higher values of
n
, e.g.
n
>= 2.
Idea:
For higher values of
n
, the function is defined in terms of itself
for a different value of
n
(which I'll
refer to as f(changed
n
) below). There are some considerations.
•
The value of
n
in the recursive call needs to change in such a way that it is working toward one (of
the) base case(s).
•
Usually we add or subtract something from
n
.
•
We occasionally multiply or divide
n
by something (illustrated much further below).
•
The value of f(
n
) is obtained by doing something to f(changed
n
), e.g. a combination of one of more
of the following:
•
Adding something to f(changed
n
).
•
Subtracting something from f(changed
n
).
•
Multiplying f(changed
n
) by something.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document •
Dividing f(changed
n
) by something.
•
If we treat subtracting
a
as adding
a
and dividing by
b
as multiplying by 1/
b,
we can summarize with
the following generalization:
f(
n
) = (f(changed
n
) +
a
) *
b + c.
Examples:
•
Assuming a base case of f(0) exists and
n
is positive,
•
f(
n
) = f(
n
1) + 1
•
f(
n
) = f(
n
1) / 5
•
f(
n
) = f(
n
1) *
n
•
Assuming base cases of f(0) and f(1) exist and
n
>= 2,
•
f(
n
) = f(
n
2) * (
n
3)
•
f(
n
) = (f(
n
2)  1)/
n
As you hopefully observed from the examples, the recursive cases make assumptions about the base cases
existing. Therefore, you should define the base case first.
Everybody's Favorite Example
No discussion of recursion would be complete without the
factorial
example from mathematics.*
Base case:
To find the base case for the factorial, we must think about what values of
n
have preset values of
n!
.
Factorial is simply defined for zero, i.e.
This is the end of the preview. Sign up
to
access the rest of the document.
This document was uploaded on 02/10/2011.
 Fall '10

Click to edit the document details