Contents    Page-10    Prev    Next    Page+10    Index   

Recursion and Runtime Stack

The runtime stack keeps a fresh set of values for each variable in a stack frame. A new stack frame is pushed onto the stack each time a function is entered. Consider a function to compute the factorial function, written n!


   (defun fact (n)
     (if (= n 0)
         1
         (* n (fact (- n 1))) ) )


>(trace fact)

>(fact 3)

  1> (FACT 3)
    2> (FACT 2)
      3> (FACT 1)
        4> (FACT 0)
        <4 (FACT 1)
      <3 (FACT 1)
    <2 (FACT 2)
  <1 (FACT 6)
6