So far, many of the methods you have used have returned:
voidA 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.
Compare these two method declarations.
public void DisplayScore()
{
System.Console.WriteLine(this.score);
}public int GetScore()
{
return this.score;
}The difference appears before the method name:
voidversus:
intRead:
public int GetScore()as:
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 informationis not the same as:
returning informationInside:
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.
Suppose:
team1.score = 3and the caller uses:
team1.GetScore();Conceptually:
caller
↓ calls GetScore()
team1
↓ reads this.score
value 3
↑ returned
callerThe method executes on team1.
The current Team object's score is 3.
That value is sent back to the caller.
If the method declares:
public int GetScore()then the method is expected to return an int-compatible value.
This fits:
return this.score;when:
scoreis an int.
The method declaration communicates the kind of value the caller should expect.
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 callerThis uses arithmetic you already know.
A field describes object state:
scoreA 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 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 → callerConsider:
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 methodLater Module 1.5 activities teach:
CO-043 uses simple methods with one clear return path.
That keeps the central idea visible: