CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Recursion

1. Recursion is a special case of Divide and Conquer problem solving
One powerful technique for solving big problems is to divide the big problem into smaller problems, each of which is easier to solve. Recursion can be used to solve problems in which these smaller subproblems are smaller versions of the original problem.

2. Writing Recursive Procedures:
Example: Write a procedure to draw n stars, where n is an integer parameter.

Strategy:

Picture:

Pascal Code:

3. Rules for Recursive procedures (or functions).

4. Classic examples of Recursion: Factorial

Pascal Code:

5. Classic examples of Recursion: Fibonacci

Fibonacci sequence: 1 1 2 3 5 8 13 ...

Each new number in the sequence is generated by adding the two previous numbers.

Pascal Code: