go to previous page   go to home page   go to next page hear noise highlighting

Answer:

3


Returned Value used by a Caller

Activations and Returns

Each caller gets a value back from the method it activated. The caller then uses this value in the addition and returns the result. The chain of activation "unwinds" as the values are returned.

public int Triangle( int N )
{
  if ( N == 1 )
    return 1;
  else
    return N + Triangle( N-1 );
}

Finally, the very first activation gets a 6 back from the method it activated, adds 4, and returns 10 to its caller.

After an activation has returned a value to its caller, it is no longer active and is removed from the chain of activations. (The diagram includes all activations in order to show the complete picture.)


QUESTION 17:

This example computed Triangle(4). How many activations of the method Triangle() were there?


go to previous page   go to home page   go to next page