4
Here is a review of how recursion works
with Triangle()
.
int Triangle( int N ) { if ( N == 1 ) return 1; else return N + Triangle( N-1 ); }
Click on the diagram shows the steps of the computation. Each activation of the method is represented by a circle. Each activation has its own parameter and returns a value to its caller.
An activation may need to wait for a returned value before it can continue with a computation. In the diagram this is represented by a circle on the chain that has not returned a value. The circle is labeled with the statement that is waiting for a result.
The bottom circle represents the base case which immediately returns a value to its caller.
When the base case is reached, values start returning to callers, and activations start completing their work.
When an activation is finished with its work, its circle is removed from the chain.
The final picture (which shows nothing) represents
the case where the first activation of Triangle()
has completed the computation and returned a value to its
caller.