1.3.8 Writing Void Methods

A Method Declaration Defines a Named Behavior

A method gives an object a named behavior that code can call.

Consider:

public void DisplayRoster()
{
    System.Console.WriteLine(this.name);
}

The first line tells C# how the method is declared:

Activity diagram showing public access modifier, then void return type, then DisplayRoster method name, then () parameter list.
PublicAccessModifier.
  • New
  • Changed
  • Removed

At this stage, the parameter list is empty. Parameters are introduced in Module 1.4.

Read the Method Before You Write the Body

The declaration tells you important things about the method before you inspect the statements inside it.

For:

public void DisplayRoster()
  • public describes the method's access;
  • void says the method does not return a value to its caller;
  • DisplayRoster names the behavior;
  • () shows the method's current parameter list.

A useful method name should help a reader predict the behavior the method is responsible for.

For example:

DisplayRoster()
ResetScore()
StartMatch()

Each name describes an action rather than a vague idea.

A void Method Performs an Action Without Returning a Value

Consider:

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

The return type is:

void

That tells C# that the method does not send a result value back to the calling code.

void does not mean that the method does nothing.

It means:

The Method Body Contains the Work

The braces contain the statements that execute when the method is called:

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

In this example, the method's job is to display the current object's name.

The declaration identifies the behavior. The body defines the instructions that perform it.

Call a Void Method as an Action

If team1 refers to a Team object, this statement:

team1.DisplayName();

asks that object to execute its DisplayName method.

The call uses the method name and parentheses:

DisplayName()

Execution moves into the method body, the statements run, and then execution continues with the statement after the call.

This connects the two sides:

Activity diagram showing method declaration, then named behavior, then method call, then method body executes, then execution returns to the caller.
MethodDeclaration.
  • New
  • Changed
  • Removed
A Void Method Can Change Object State

A method does not need to return a value in order to have an effect.

For example:

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

This method returns no value.

It still changes the current Team object's state by assigning 0 to score.

So these are different questions:

  • Does the method perform an action? It can.
  • Does the method change object state? It can.
  • Does the method return a value to the caller? A void method does not.
Keep the Current Method Shape Simple

For now, write methods with empty parameter lists:

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

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

The parentheses still matter even though there is nothing between them.

They are part of the method declaration and appear again when the method is called:

team1.ResetScore();
team1.DisplayName();
Choose a Declaration That Matches the Required Behavior

Suppose the requirement is:

A focused method could be:

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

The declaration and body work together:

  • ResetScore communicates the action;
  • void indicates that no result value is returned;
  • () shows that the method currently receives no parameters;
  • the body performs the required state change.
Read a Method as One Complete Unit

When you encounter a method, ask:

  1. What is the method called?
  2. What access does it have?
  3. What does its return type tell me?
  4. Does it currently accept any parameters?
  5. What statements run inside the body?
  6. What observable action or state change does the method perform?

For the methods in this module, the most important pattern is:

public void MethodName()
{
    action statements
}
The Core Idea

A well-written void method gives one focused behavior a clear name and a clear set of instructions.

The declaration tells you how the method is identified and that it returns no value. The body tells you what the method actually does.

When another object or statement calls the method, that behavior executes and control then returns to the caller.