1.4.7 Understanding Method Parameters

Parameters Let a Method Receive Information from Its Caller

A method without parameters has empty parentheses:

public void DisplayName()
{
}

A method with one parameter declares information inside the parentheses:

public void SetJerseyNumber(int number)
{
    this.jerseyNumber = number;
}

The parameter gives the method a named way to work with information supplied by the caller.

A Parameter Has a Type and a Name

For:

int number

the type is int.

The parameter name is number.

The type tells C# what kind of input the method expects.

The name gives the method a way to refer to that input.

The Argument Supplies the Parameter Value

A call such as:

player1.SetJerseyNumber(7);

supplies:

7

to:

number

Conceptually:

Activity diagram showing caller, then argument 7, then SetJerseyNumber(int number), then inside method: number is 7.
Caller.
  • New
  • Changed
  • Removed
Parameter Values Belong to a Particular Call

Suppose the code executes:

player1.SetJerseyNumber(7);
player1.SetJerseyNumber(9);

The method is called twice.

During the first call:

number = 7

During the second call:

number = 9

Each call supplies its own parameter value.

The Method Can Use the Parameter in Its Statements

For example:

public void SetJerseyNumber(int number)
{
    this.jerseyNumber = number;
}

The statement:

this.jerseyNumber = number;

uses two different kinds of names.

this.jerseyNumber is a field on the current object.

number is the parameter for the current method call.

The assignment moves information from the parameter into object state.

A Boolean Parameter Works the Same Way

Consider:

public void SetAvailability(bool available)
{
    this.isAvailable = available;
}

Call:

player1.SetAvailability(true);

During the call:

available = true

The parameter type is bool, so the incoming argument must be compatible with a Boolean value.

Parameters Make Methods More General

Without a parameter:

public void SetJerseyNumber()
{
    this.jerseyNumber = 7;
}

the method always assigns 7.

With a parameter:

public void SetJerseyNumber(int number)
{
    this.jerseyNumber = number;
}

the caller can choose the value:

player1.SetJerseyNumber(7);
player2.SetJerseyNumber(12);
Parameters Are Part of the Method Signature

Compare:

SetJerseyNumber()

with:

SetJerseyNumber(int number)

The parameter changes the method declaration.

Later UML Learning Activities show parameters in sequence diagrams and class diagrams.

A Parameter Is Available Inside Its Method

The name:

number

exists for the method body where it is declared.

Code outside that method cannot simply use number as though it were a field on the object.

This leads directly to scope.

Keep the Current Scope to One Parameter

Methods with multiple parameters are taught later.

For now:

Object diagram showing caller chooses one argument, then one parameter receives that value, then method uses the parameter, then method may change object state.
CallerChoosesOneArgument.
  • New
  • Changed
  • Removed