# Class 21: Recursion

Back to Algorithm Analysis. On to Recursion, Revisited.

Held Thursday, September 30, 1999

Overview

Today, we will ground our learning of recursion in Java by experimenting with two recursive functions, one that computes Fibonacci numbers and one that does exponentiation. In each case, we will consider both inefficient and more efficient versions.

Notes

• Don't forget tomorrow's talk!
• Don't forget today's cool convocation talk.

Contents

Handouts

Summary

• Today:
• Lab A1; Recursion
• Short discussion:
• Recursive functions
• Assigned (for tomorrow):
• Lab A2: Searching

## Lab

Do A1.3 and A1.4.

## Recursion

• You've studied Scheme, so you should know about recursion.
• As you've observed, it's not much different in Java.
• A recursive function is a function which calls itself, either directly or indirectly.
• A function calls itself indirectly by calling another function which calls the original function.
• Recursion provides one mechanism for looping.
• Many mathematicians and computer scientists consider recursion significantly more elegant than for or while loops.
• The body of a typical recursive function looks like
```if (some-test) {
base-case;
} // base case
else {
call-the-same-function-again-with-modified-parameters;
} // recursive case
```

## History

Tuesday, 10 August 1999

• Created as a blank outline.

Tuesday, 28 September 1999

Wednesday, 29 September 1999

• Updated a few details.

Back to Algorithm Analysis. On to Recursion, Revisited.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.