Contents    Page-10    Prev    Next    Page+10    Index   

Recursive Function Execution

Consider the computation of (fact 3). A new stack frame is created in which n = 3:

n = 3

Now we can execute the code of fact. We test (if (<= n 0) ...) and, since n = 3, evaluate (* n (fact (- n 1))). n evaluates to 3, and then we evaluate (fact (- n 1)) which is (fact 2). This creates a new stack frame in which n = 2:

n = 2
n = 3

Note that the older binding, n = 3, has not gone away, but is now shadowed by a new binding n = 2.

Now we test (if (<= n 0) ...) and call (fact 1):

n = 1
n = 2
n = 3

and then we call (fact 0):

n = 0
n = 1
n = 2
n = 3