Back

Activity 2.2: Parameters

divider

Activity 2.2

Parameters

Key Concepts

One Method, Many Inputs

Parameters and Arguments

Matched by Position

A Parameter That Changes What Happens

Activity 2.1's Methods Do the Same Thing Every Time

PrintBanner always printed MISSION CONTROL. Countdown always started at 3. A method from Activity 2.1 cannot be asked to do its job a little differently — it does exactly one thing.

So here is what welcoming two crew members looks like with only Activity 2.1's tools.

Activity 2.1's Methods Do the Same Thing Every Time

Demo
GreetAvery();
GreetBlake();
void GreetAvery()
{
Console.WriteLine("Welcome aboard, Avery.");
}
void GreetBlake()
{
Console.WriteLine("Welcome aboard, Blake.");
}

Two methods that differ by one word. A crew of seven would need seven, and a crew member whose name you only learn when the program runs could not be welcomed at all. Activity 2.1 removed repeated blocks; this is the repetition it left behind.

Leave a Blank, Fill It In When You Call

The fix is to write the method once with a blank where the name goes. Inside the header's parentheses — empty in Activity 2.1 — you write string name: a type and a name, exactly like declaring a variable in Activity 1.5.

Each call then puts a value in its parentheses, and name holds that value while the body runs. Predict the three lines this prints.

Leave a Blank, Fill It In When You Call

Demo
Greet("Avery");
Greet("Blake");
Greet("Casey");
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}
Demo
Welcome aboard, Avery.
Welcome aboard, Blake.
Welcome aboard, Casey.

One method, three different lines. On the first call name is "Avery", on the second "Blake", on the third "Casey". Line 7 never changes; what fills its blank does.

Two Words for the Two Ends of the Blank

Parameter — name, the variable in the method's header. It is the blank, and it has a type.

Argument — "Avery", the value in the call's parentheses. It is what fills the blank, this time.

One parameter, many arguments: name appears once in the file, and every call brings its own argument. The exam uses both words and expects you to know which is which.

A Parameter Lives Only Inside Its Method

name exists while Greet's body runs, and only there. Each call makes a fresh name holding that call's argument, and it disappears when the body ends.

That is why three calls cannot interfere with each other. The "Avery" from the first call is gone before "Blake" arrives. Activity 2.5 takes this idea further.

Two Parameters Are Matched by Position

A method can take more than one parameter, separated by commas, each with its own type. The first argument fills the first parameter, the second fills the second — by position, never by meaning.

Both calls below build. Predict both lines, and decide which one is nonsense.

Two Parameters Are Matched by Position

Demo
CrewLine("Avery", "Captain");
CrewLine("Captain", "Avery");
void CrewLine(string name, string role)
{
Console.WriteLine($"{name} - {role}");
}
Demo
Avery - Captain
Captain - Avery

Line 2 runs and prints nonsense, with no error. C# only knows that both parameters are strings, and "Captain" is a perfectly good string. Nothing in the language knows a rank is not a name.

C# Catches a Swap Only When the Types Differ

Now the two parameters have different types: a string for the name and an int for the seat. Predict what happens to the swapped call on line 2.

Demo
AssignSeat("Avery", 1);
AssignSeat(1, "Avery");
void AssignSeat(string name, int seat)
{
Console.WriteLine($"{name} takes seat {seat}.");
}

C# Catches a Swap Only When the Types Differ

Demo
AssignSeat("Avery", 1);
AssignSeat(1, "Avery");
void AssignSeat(string name, int seat)
{
Console.WriteLine($"{name} takes seat {seat}.");
}
Demo
2-2-crew-boarding.cs(2,12): error CS1503: Argument 1: cannot convert from 'int' to 'string'
2-2-crew-boarding.cs(2,15): error CS1503: Argument 2: cannot convert from 'string' to 'int'
The build failed. Fix the build errors and run again.

It refuses to build, once for each argument: a number cannot fill a string blank, and text cannot fill an int one. The types caught this swap. They could not catch the last one, because there both blanks were the same type.

A Blank Cannot Be Left Empty

Greet has a parameter. Predict what happens if a call gives it no argument at all.

Demo
Greet();
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}

A Blank Cannot Be Left Empty

Demo
Greet();
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}
Demo
2-2-crew-boarding.cs(1,1): error CS7036: There is no argument given that corresponds to the required parameter 'name' of 'Greet(string)'
The build failed. Fix the build errors and run again.

C# will not run a call with a blank unfilled. The message names the parameter that is missing, name, and the method that needed it. Every parameter needs an argument, every time.

A Parameter Can Change What the Method Does

Greet only drops its argument into a sentence. A parameter can do more than that: it can decide how many times a loop runs. Here is Activity 2.1's Countdown with one change — the loop starts at from instead of at 3.

Predict how many lines the two calls print.

A Parameter Can Change What the Method Does

Demo
Countdown(3);
Countdown(5);
void Countdown(int from)
{
for (int i = from; i >= 1; i--)
{
Console.WriteLine($"T-minus {i}");
}
Console.WriteLine("LIFTOFF!");
}
Demo
T-minus 3
T-minus 2
T-minus 1
LIFTOFF!
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
LIFTOFF!

Four lines, then six. The same body ran a different number of times, because from set where the loop began. The argument did not just appear in the output — it changed what the method did.

This Is a Create Task Requirement

The Create Performance Task asks for a procedure you wrote that uses one or more parameters that “have an effect on the functionality of the procedure.”

Countdown qualifies: change from and the loop runs a different number of times. Whether Greet does is a real question — its argument changes the output, but it never decides anything. You will argue it in today's reflection.

You are writing the thing the task asks for, months before the task.

Today's Objectives

  • Defining a method with one or more typed parameters
  • Telling a parameter from an argument
  • Predicting what swapped and missing arguments do, and which ones C# catches
  • Using a parameter to change what a method does, not just what it prints

Key Terms

Parameter
A variable in a method's header, with a type. It is the blank the method fills in from each call.
Argument
The value written in a call's parentheses. It fills the matching parameter for that one call.
Matched by position
The first argument goes to the first parameter, the second to the second, whatever their names mean.

'F' → Fullscreen

divider

Build

Today you board the crew for launch in a new program named 2-2-crew-boarding. Every crew member is welcomed, listed, and seated by the same three methods, and the countdown can start from any number.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.


Task 1: One Blank

  • Write the code below and run it. name on line 4 is the parameter; "Avery" and "Blake" are the arguments.
  • Add a third call with your own name as the argument, and run it. Then take that call back out.
2-2-crew-boarding
Greet("Avery");
Greet("Blake");
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}

Task 2: Two Blanks, Same Type

  • Add CrewLine below Greet, and the two calls on lines 3 and 4. Run it.
  • Swap the two arguments on line 3 so it reads CrewLine("Pilot", "Avery"); and run it. Write down exactly what printed, and whether there was an error. Then swap them back.
2-2-crew-boarding
Greet("Avery");
Greet("Blake");
CrewLine("Avery", "Pilot");
CrewLine("Blake", "Engineer");
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}
void CrewLine(string name, string role)
{
Console.WriteLine($"{name} - {role}");
}

Task 3: Two Blanks, Different Types

  • Add AssignSeat and its two calls. Its second parameter is an int, so its argument is a number with no quotes.
  • Swap the two arguments on line 5 so it reads AssignSeat(1, "Avery"); and run it. Write down the error code, how many errors there were, and what each one said it could not convert. Then swap them back.
2-2-crew-boarding
Greet("Avery");
Greet("Blake");
CrewLine("Avery", "Pilot");
CrewLine("Blake", "Engineer");
AssignSeat("Avery", 1);
AssignSeat("Blake", 2);
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}
void CrewLine(string name, string role)
{
Console.WriteLine($"{name} - {role}");
}
void AssignSeat(string name, int seat)
{
Console.WriteLine($"{name} takes seat {seat}.");
}

Task 4: A Countdown That Starts Anywhere

  • Add the three new lines to the main program. The program will not build yet: lines 7 and 9 call a method that does not exist.
  • Replace the comments at the bottom with Countdown. It is Activity 2.1's countdown with one parameter, int from, and the loop starts at from instead of at 3.
  • Run it and check it against the Checkpoint below: four countdown lines the first time, six the second, from one method.
2-2-crew-boarding
Greet("Avery");
Greet("Blake");
CrewLine("Avery", "Pilot");
CrewLine("Blake", "Engineer");
AssignSeat("Avery", 1);
AssignSeat("Blake", 2);
Countdown(3);
Console.WriteLine("Launch scrubbed. Resetting the clock.");
Countdown(5);
void Greet(string name)
{
Console.WriteLine($"Welcome aboard, {name}.");
}
void CrewLine(string name, string role)
{
Console.WriteLine($"{name} - {role}");
}
void AssignSeat(string name, int seat)
{
Console.WriteLine($"{name} takes seat {seat}.");
}
// Write a method named Countdown here. It takes one int parameter named from.
// It counts down from "from" to 1, one "T-minus" line each,
// then prints LIFTOFF! -- Activity 2.1's countdown, but starting at "from".

Task 5: Break It on Purpose

Three changes, one at a time. Write down exactly what happens, and put each one back before the next.

  1. Change line 1 to Greet(); — no argument.
  2. Change line 1 to Greet(Avery); — the quotes gone.
  3. Change line 7 to Countdown(0);.

Two of these stop the build. One builds and runs. For the two errors, write the code and say in your own words what C# was objecting to. For the one that runs, write what it printed in place of the first countdown, and say why C# had no reason to object.

There is no code block for this task. When it is over, your program is exactly what it was at the end of Task 4.


Challenge (Optional): One Second, Thirty Seconds

  • Write a method Hold with one int parameter, seconds. It prints Holding for 30 seconds. — but for 1 it must say Holding for 1 second., with no s.
  • You need an if inside the method that looks at the parameter. Call it with 1 and with 30, and check both.
  • This parameter changes which line runs, not just what it prints. That is the strongest kind of “effect on the functionality.”
divider

Same Idea, Exam Notation

The reference sheet writes a procedure with parameters as PROCEDURE procName(parameter1, parameter2, ...) and a call as procName(arg1, arg2, ...).

C#
Greet("Avery");
void Greet(string name)
{
Console.WriteLine($"Welcome, {name}");
}
Exam Notation
PROCEDURE greet(name)
{
DISPLAY("Welcome,")
DISPLAY(name)
}
greet("Avery")

1. Exam Parameters Have No Types

PROCEDURE greet(name) — just the name. C# makes you write string name, and that type is exactly what let C# catch Task 3's swap. On the exam nothing catches either swap, so read the order of the arguments carefully.

2. Arguments Are Matched by Position, Both Places

The reference sheet says it directly: arg1 is assigned to parameter1, arg2 to parameter2, and so on. Task 2's swap means the same thing on the exam as it did in your program.

3. The Exam Has No Interpolation

There is no $"..." on the reference sheet. To show a word and a value on one line, the exam uses two DISPLAY calls, and DISPLAY adds a space after each value, so the panel prints Welcome, Avery with the space supplied by the first call.

divider

Checkpoint

The finished program's output.

Example Output
Welcome aboard, Avery.
Welcome aboard, Blake.
Avery - Pilot
Blake - Engineer
Avery takes seat 1.
Blake takes seat 2.
T-minus 3
T-minus 2
T-minus 1
LIFTOFF!
Launch scrubbed. Resetting the clock.
T-minus 5
T-minus 4
T-minus 3
T-minus 2
T-minus 1
LIFTOFF!
  • Every crew line comes from one of three methods, each defined once
  • Countdown has one body, and prints four lines for 3 and six for 5
  • Every method is below the main program
  • You recorded the Task 2 swap, the Task 3 errors, and all three Task 5 results
divider

Reflection

Answer the following questions before submitting your work.

  1. In Greet("Avery") and its definition, which is the parameter and which is the argument? Say where each one appears in your program.
  2. Task 2's swap printed nonsense with no error, but Task 3's swap would not build. Explain what C# could check in Task 3 that it could not check in Task 2.
  3. The Create task needs a procedure whose parameter has an effect on its functionality. Does Greet qualify? Does Countdown? Argue each in a sentence.
divider

Submit

Submit four things to the dropbox:

  1. Your 2-2-crew-boarding program file.
  2. Your Task 2 swap output and your Task 3 errors.
  3. All three Task 5 results, with what each meant.
  4. Your three reflection answers.

Activity Complete