Returned Value used by a Caller

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

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.

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, so it computes a 10 and returns that 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 does not show this.

QUESTION 15:

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