1.5.1 Returning Values From Methods

Methods Can Produce Information for Their Callers

So far, many of the methods you have used have returned:

void

A void method performs behavior but does not send a data value back to the code that called it.

A method can also be designed to return a value.

For example, a soccer Team object might provide its current score:

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

This method produces an int value for its caller.

The Return Type Appears Before the Method Name

Compare these two method declarations.

A void method
public void DisplayScore()
{
    System.Console.WriteLine(this.score);
}
A method that returns an int
public int GetScore()
{
    return this.score;
}

The difference appears before the method name:

void

versus:

int

Read:

public int GetScore()

as:

A Returned Value Is Different from Displaying a Value

Consider:

public void DisplayScore()
{
    System.Console.WriteLine(this.score);
}

This method displays the score.

It does not give the score back to the caller as a returned value.

Now compare:

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

This method sends the score back to the caller.

The caller can then decide what to do with that value.

That distinction is important:

displaying information

is not the same as:

returning information
Read a Simple return Statement

Inside:

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

the statement:

return this.score;

means:

At this stage, focus on recognizing a simple return from a straightforward method.

Later activities teach more complex return behavior.

The Returned Value Travels Back to the Caller

Suppose:

team1.score = 3

and the caller uses:

team1.GetScore();

Conceptually:

caller
  ↓ calls GetScore()
team1
  ↓ reads this.score
value 3
  ↑ returned
caller

The method executes on team1.

The current Team object's score is 3.

That value is sent back to the caller.

The Return Type and Returned Value Must Be Compatible

If the method declares:

public int GetScore()

then the method is expected to return an int-compatible value.

This fits:

return this.score;

when:

score

is an int.

The method declaration communicates the kind of value the caller should expect.

A Method Can Return a Simple Calculation

A return value does not always have to be a field by itself.

Suppose a Team stores:

public int homeGoals;
public int awayGoals;

A simple supplied method might be:

public int GetTotalGoals()
{
    return this.homeGoals + this.awayGoals;
}

The expression is evaluated first.

Then its result is returned.

Conceptually:

homeGoals + awayGoals
        ↓
numeric result
        ↓
return to caller

This uses arithmetic you already know.

A Return Is Part of Method Behavior

A field describes object state:

score

A method can expose behavior that produces information:

GetScore()

This supports the object-oriented idea that an object can provide information through its methods rather than requiring every caller to know how that information is stored internally.

The Caller Receives the Result

The returned value becomes available at the place where the method was called.

A caller can store it in a local variable:

int currentScore = team1.GetScore();

That call pattern is the focus of another page in this batch.

For now, recognize the direction:

caller → method call
method → returned value → caller
Do Not Confuse Return Type with Parameter Type

Consider:

public int GetScore()

The int before GetScore is the return type.

It describes what the method sends back.

Now compare:

public void SetScore(int score)

The int score inside the parentheses is a parameter.

It describes information sent into the method.

The directions are opposite:

parameter → information into method
return    → information out of method
Do Not Add Decision Logic Yet

Later Module 1.5 activities teach:

  • returns inside conditional logic;
  • methods that must return on every code path;
  • multiple and early returns;
  • debugging conditional returns.

CO-043 uses simple methods with one clear return path.

That keeps the central idea visible: