Java Reference
In-Depth Information
Chapter
15
Recursion
OBJECTIVES
• Learn what
recursion
means and see how it applies to methods.
• Learn the two perspectives on recursive calls: how to execute them and
how to understand them.
• Study some interesting recursive methods.
• Learn about object recursion.
• Develop a skill in writing recursive methods.
INTRODUCTION
A recursive definition is a definition that defines something in terms of itself. For
example, a
noun phrase
could be defined as either a
noun
or an
adjective
fol-
lowed by a
noun phrase
. Mathematics is rampant with recursive definitions be-
cause using recursion is often the easiest way to define something.
Recursion is a powerful tool in programming. For some tasks, using recur-
sive methods is much easier than using loops. In fact, there are
functional pro-
gramming languages
that rely completely on recursion and do not have loops.
In this chapter, we study recursion and show how useful it is.
15.1
The recursive pattern
15.1.1
A simple recursive definition
A
noun phrase
is a series of adjectives (possibly empty) followed by a noun.
Since
dog
is a noun,
dog
is also a noun phrase —here, the series of adjectives is
empty. Since
the
,
big
, and
brown
are adjectives,
the big brown dog
is also a noun
phrase.
Activity
15-1.1
Search WWH ::
Custom Search