1.5.3 Calling Methods That Return Values

A Returned Value Becomes Available at the Call Site

Suppose a Team class provides:

public int GetScore()
{
    return this.score;
}

The method produces an integer.

When you call:

team1.GetScore()

the call produces that returned integer at the point where the call appears.

One of the clearest ways to use it is to store the result in a local variable.

Store the Returned Value in a Compatible Local Variable

Consider:

int currentScore = team1.GetScore();

Read the statement from right to left.

Method call
team1.GetScore()

Ask team1 to run GetScore().

Returned value

If:

team1.score = 3

the method sends back:

3
Local variable
int currentScore

stores the returned integer.

Conceptually:

team1.GetScore()
      ↓
returned value 3
      ↓
currentScore = 3
The Local Variable Type Should Match the Returned Type

If the method is declared:

public int GetScore()

then this is a natural match:

int currentScore = team1.GetScore();

The method returns an int.

The local variable stores an int.

The Method Call Happens Before the Assignment Finishes

For:

int currentScore = team1.GetScore();

execution needs the method result before the local variable can receive its value.

The flow is:

call GetScore()
    ↓
method executes
    ↓
value returned
    ↓
initialize currentScore

The assignment cannot finish until the returning method finishes.

The Returned Value Is a Result, Not the Method Itself

Suppose:

int currentScore = team1.GetScore();

After the call completes:

currentScore

contains the integer value returned by the method.

It does not contain:

GetScore()

and it does not contain the entire Team object.

It contains the returned result.

Different Objects Can Produce Different Results

Suppose:

team1.score = 3
team2.score = 1

Then:

int team1Score = team1.GetScore();
int team2Score = team2.GetScore();

can produce:

team1Score = 3
team2Score = 1

The same method design executes on different current objects.

The object's state affects the value that is returned.

A Returned Value Can Be Used Later in the Same Method

After:

int currentScore = team1.GetScore();

later code in the same scope can use:

currentScore

For example:

System.Console.WriteLine(currentScore);

The method call has already completed.

The local variable now holds the result.

Do Not Confuse a Returning Method with a void Method

This is valid when GetScore() returns an integer:

int currentScore = team1.GetScore();

But a void method such as:

team1.DisplayScore();

does not produce a data value to store in an int local variable.

The method's return type determines how the caller can use it.

Trace the Call with the Debugger

Suppose execution reaches:

int currentScore = team1.GetScore();

A useful trace is:

  1. inspect team1;
  2. predict what GetScore() should return;
  3. step into GetScore();
  4. inspect this.score;
  5. observe the simple return;
  6. return to the caller;
  7. inspect currentScore.

If:

this.score = 3

you should expect:

currentScore = 3

after the call completes.

The Call Stack Shows the Temporary Method Nesting

While execution is inside GetScore(), the Call Stack can show that:

GetScore()

is currently active above its caller.

When GetScore() finishes, execution returns to the calling method and the returned value becomes available there.

Keep CO-043 Calls Simple

Later activities teach more advanced uses of returned values.

For now:

  • call one returning method;
  • receive one simple value;
  • store it in a local variable;
  • trace where the value came from.

Do not introduce:

  • object returns;
  • passing a returned value directly as another method's argument;
  • complex nested method-call expressions;
  • conditional return paths.
The Main Call Pattern
ReturnType localVariable = objectReference.MethodName();

Example:

int currentScore = team1.GetScore();

Read it as: