# 11. Recursion

#### Thursday October 08, 1998

[Previous Lecture] [Next Lecture]

Assigned Reading:  online notes on recursion

Handouts (available on-line):

Topics Covered:

• We start looking at recursion. The first few programs we discuss on recursion are "mickey mouse" examples. You probably would not use recursion to write these programs. But, soon we will see that there are problems which requires us to think recursively.

• The first program using recursion simply demonstrates that a function can call itself. Sample run.

• The second program using recursion simply demonstrates that syntactically correct recursive functions may still contain an infinite loop Sample run.

• The third program using recursion shows the effect of recursive calls on local variables (which is "none"). Sample run.

• We develop a recursive function to compute n! (read "n factorial").

• The first program does not use any recursion. The function iter_fact uses a for loop to compute n!. Sample run.

• In the second program the function rec_fact computes n! by returning the value n * iter_fact(n-1) . If you believe that iter_fact works correctly, you should also believe that rec_fact works correctly. Sample run. Actually, this program does not compute 0! correctly, since 0! should be 1. This is in fact a good point, since the reason rec_fact does not work for 0, is because we cannot use iter_fact to compute (-1)!.

• What if iter_fact does not work correctly? In the third program we put a bug in iter_fact so that it crashes if it is called with a parameter > 7. However, since iter_fact still works for parameters <= 7, the rec_fact function still works for values <= 8. Sample run.

• Finally, we look at a program with a recursive function that computes n!. To prove that this function works correctly, we use mathematical induction. First, we notice that the if statement guarantees that rec_fact computes n! correctly for 0! and 1!. This then guarantees that it computes 2! correctly, which in turn guarantees that 3! is correct, and so on and so forth. Sample run.

• The Fibonacci numbers are defined recursively, so we can easily write a program with a recursive function that computes the nth Fibonacci number. ( Sample run.)

• Note the use of argc and argv in the parameter list of the main function. These parameters allow us to access the command line arguments that were typed in at the UNIX prompt.

• This program is really slow. It took 3 minutes and 22 seconds to compute the 42nd Fibonacci number. The reason is that the fib() function is called over and over again for the same values. For example, to compute fib(n), the value of fib(1) is computed fib(n-1) times. Since the Fibonacci numbers grow very quickly, this makes our program very slow.

• One way to fix the problem of repeatedly computing the same values of fib(i) is to use a memoization table. (Note: the word is not "memorization".) In this approach, after we compute the value of fib(i) for the first time, we record it in the memoization table. If the function fib() is called with parameter i again, the value from the table is retrieved and returned. This approach reduces the number of recursive calls dramatically and gives us a much faster program. The sample run shows that the new running time to compute fib(42) is less than 0.1 seconds.

[Previous Lecture] [Next Lecture]