1.4.26 Passing Arguments to Method Parameters

A Method Call Can Send Information into a Method

A method can declare one or more parameters:

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

The parameter:

number

is ready to receive a value when the method is called.

A call supplies that value as an argument:

player1.SetJerseyNumber(7);

The argument:

7

is passed into the parameter:

number
Trace the Value from Caller to Method

A useful mental model is:

Activity diagram showing caller, then argument, then parameter, then method statement.
Caller.
  • New
  • Changed
  • Removed

For:

player1.SetJerseyNumber(7);

and:

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

the flow is:

Activity diagram showing 7, then number, then this.jerseyNumber.
.
  • New
  • Changed
  • Removed

The caller supplies the value.

The method receives it through the parameter.

The method can then use the parameter in its own logic.

Multiple Parameters Receive Multiple Arguments

Suppose:

public void SetPlayerInfo(string name, int number)
{
    this.name = name;
    this.jerseyNumber = number;
}

A matching call is:

player1.SetPlayerInfo("Jordan", 7);

The arguments match the parameters by position:

Activity diagram showing "Jordan" name, then 7 number.
JordanName.
  • New
  • Changed
  • Removed

The first argument is used for the first parameter.

The second argument is used for the second parameter.

Type Compatibility Still Matters

If the method expects:

int number

then this call is compatible:

player1.SetJerseyNumber(7);

This call is not:

player1.SetJerseyNumber("seven");

The argument must be compatible with the parameter type.

The same rule applies when parameters use:

string
bool
doublebool
double

or another available type.

Arguments Can Be Literals

A literal is a value written directly in the call.

Examples:

player1.SetJerseyNumber(7);
player1.SetAvailability(true);
player1.SetPosition("Forward");

The literals are:

7
true
"Forward"

Each one becomes the value of the matching parameter during the call.

Arguments Can Be Variables

Suppose a method already has:

int selectedNumber = 9;

The local variable can be used as an argument:

player1.SetJerseyNumber(selectedNumber);

The current value stored in:

selectedNumber

is supplied to the method parameter.

Conceptually:

Activity diagram showing selectedNumber, then current value 9, then number parameter.
SelectedNumber.
  • New
  • Changed
  • Removed
Arguments Can Be Fields

If:

team1.defaultJerseyNumber

is an int, it can be used where an int argument is expected:

player1.SetJerseyNumber(team1.defaultJerseyNumber);

The argument expression reads the current field value.

The method receives that value through its parameter.

A Parameter Gets Its Own Method-Level Value

Consider:

int selectedNumber = 9;
player1.SetJerseyNumber(selectedNumber);

Inside:

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

the parameter is named:

number

The caller's local variable is named:

selectedNumber

The names do not have to match.

The value is what moves across the method-call boundary.

Passing an Argument Does Not Automatically Change the Caller Variable

Suppose:

int selectedNumber = 9;
player1.SetJerseyNumber(selectedNumber);

The method receives the value through its parameter.

Changing the Player field does not automatically change:

selectedNumber

The local variable and the object field are different storage locations.

A Parameter Can Become an Argument in Another Method Call

A method can receive a value through a parameter and then use that parameter as the argument in another method call.

This is called forwarding an argument.

Conceptually:

Activity diagram showing original caller, then argument, then first method parameter, then forwarded as argument, then second method parameter.
OriginalCaller.
  • New
  • Changed
  • Removed

The same information can move through more than one method call.

Forward an Incoming Value to Another Object

Suppose Team has:

public void DisplayMessage(string message)
{
    System.Console.WriteLine(message);
}

Player has a Team reference:

public Team team;

Player can define:

public void SendTeamMessage(string message)
{
    this.team.DisplayMessage(message);
}

The Player method receives message.

Then it passes that value to the Team method.

Trace a Forwarded Argument

Suppose:

player1.SendTeamMessage("Practice starts at 5.");

The first call supplies:

"Practice starts at 5."

to the Player parameter:

message

Inside SendTeamMessage, the code calls:

this.team.DisplayMessage(message);

The current value of message becomes the argument for DisplayMessage.

The information flow is:

Activity diagram showing "Practice starts at 5.", then Player.SendTeamMessage(string message), then message, then Team.DisplayMessage(message), then Team method receives the same text.
PracticeStartsAt.
  • New
  • Changed
  • Removed

The Player method does not need to create a new text value. It can forward the value it already received.

The Same Name Can Have Two Roles Across Call Boundaries

Inside:

public void SendTeamMessage(string message)

message is a parameter.

Inside this call:

this.team.DisplayMessage(message);

that same expression is being used as the argument supplied to another method.

The role depends on which side of the method-call boundary you are looking at.

Keep the Receiver and the Argument Visible

In:

this.team.DisplayMessage(message);

there are two important pieces.

Receiver
this.team

Which object receives the call?

Argument
message

Which value is sent to the receiving method?

Tracing both makes object-to-object method calls easier to follow.

Forwarding Does Not Automatically Store the Value

Suppose:

public void SendTeamMessage(string message)
{
    this.team.DisplayMessage(message);
}

The Player receives the text and passes it onward.

The Player does not automatically keep that text as object state.

If the class needs to remember the value later, the design would need a field assignment that explicitly stores it.

Parameters and fields remain different.

Forwarding Can Cross Object Boundaries

In the example:

Activity diagram showing caller, then player1.SendTeamMessage(...), then team1.DisplayMessage(...).
Caller.
  • New
  • Changed
  • Removed

the value moves from the caller into Player behavior and then into Team behavior.

This connects parameters with the object-to-object method calls you learned earlier.

Connect the Call to UML

A specific call can be represented conceptually in a sequence diagram as:

controller1 ── SetJerseyNumber(7) ──> player1

A forwarded call can also show the value continuing to another object:

Sequence diagram showing controller1 player1 : Player team1 : Team, then SendTeamMessage("Practice starts at 5."), then DisplayMessage("Practice starts at 5.").
Controller1Player1PlayerTeam1Team.
  • New
  • Changed
  • Removed

A class diagram can show the method parameter:

SetJerseyNumber(number : int) : void

C# connects those representations through the actual method calls.

Predict and Trace Parameter Values

When debugging a parameterized method:

  1. identify the argument;
  2. predict the parameter value;
  3. step into the method;
  4. inspect the parameter;
  5. if the parameter is forwarded, identify the next call and receiver;
  6. predict which parameter receives the forwarded value;
  7. compare the debugger state with your prediction.

That turns method calls into traceable value flow rather than mysterious jumps between source files.

Keep Forwarding to Existing Values for Now

Later modules introduce methods that return values and allow a returned value to become another argument.

That is not needed here.

For now, forward a value that the current method already received as a parameter.

The Main Pattern

Keep this relationship clear:

Activity diagram showing argument expression, then matching parameter, then method uses the received value, then parameter may become an argument, then another method receives the value.
ArgumentExpression.
  • New
  • Changed
  • Removed