1.3.5 Void Methods

A void Method Performs Behavior Without Returning a Value

A method declaration such as:

C#
public void DisplayName()
{
    System.Console.WriteLine(this.name);
}

uses the return type:

Plain text
void

That tells C# the method does not return a value to the calling code.

Call a Void Method as an Action

If:

C#
team1.DisplayName();

executes, the call asks the Team object to perform the behavior.

After the method finishes, execution continues with the next statement after the call.

A Void Method Can Still Change State

For example:

C#
public void ResetScore()
{
    this.score = 0;
}

The method returns no value.

It still changes the current Team object's state.

So:

Plain text
void

does not mean:

the method does nothing

It means:

the method does not send a result value back to the caller.

Keep Current Examples Without Parameters

Parameters are introduced in Module 1.4.

For now, use methods such as:

C#
ResetScore()
DisplayName()
StartMatch()

with empty parentheses.

Use void When the Current Purpose Is an Action

If the method's job is:

Reset the score.

a void method is a natural introductory design.

Methods that calculate and return a value are taught later.

Methods Prepare You for Sequence Diagrams

A sequence diagram can show one object calling a void method on another object.

That visual model is the focus of CO-035.