The problem: repeated code
Imagine a teacher who asks three students, Andy, Mike and Sally, to write an apology on the board. Without parameters, you would write every line for every student:
Console.WriteLine("This is Andy");
Console.WriteLine("Andy made a mistake");
Console.WriteLine("Andy suffers the consequences");
Console.WriteLine("This is Mike");
Console.WriteLine("Mike made a mistake");
Console.WriteLine("Mike suffers the consequences");
Console.WriteLine("This is Sally");
Console.WriteLine("Sally made a mistake");
Console.WriteLine("Sally suffers the consequences");
It works, but the three blocks are the same text with one word changed. If a name changes, you edit three lines. With ten students and a longer apology, you would be copying and editing dozens of lines, and one missed edit would leave the wrong name in the output.
The fix: a method with a parameter
A parameter is a variable declared in a method's header. It is a placeholder for a value that the caller supplies. An argument is the actual value passed in a call. In Apologize("Andy"), "Andy" is the argument, and inside the method it is stored in the parameter.
public void Apologize(string name) {
Console.WriteLine("This is " + name);
Console.WriteLine(name + " made a mistake");
Console.WriteLine(name + " suffers the consequences");
}
public void MainFunction() {
Apologize("Andy");
Apologize("Mike");
Apologize("Sally");
}
The text is written once. Each call fills in name with a different argument and runs the same three lines. The output for the first call is:
This is Andy
Andy made a mistake
Andy suffers the consequences
Now both kinds of change are cheap. To use different students, change only the arguments in the calls. To reword the apology, change it once inside the method, and every call picks up the new wording.
Syntax and C# details
A method header lists each parameter with its type and its name, separated by commas:
public void IntroduceYourself(string name, string language)
- Types are required. C# is statically typed, so each parameter declares its type. Passing a value of the wrong type, such as a number where a
stringis expected, is a compile-time error. - Arguments match by position. In
IntroduceYourself("John", "Python"), the first argument goes tonameand the second tolanguage. Swap them and the code still compiles, but the output is wrong. - The count must match. Calling a two-parameter method with one argument does not compile, unless the method gives that parameter a default value.
- Naming. C# convention gives methods PascalCase names such as
IntroduceYourself, and parameters camelCase names such asname.
Join strings before printing them
A common slip is to pass the pieces of a message to Console.WriteLine as separate arguments, the way some other languages allow. In C#, Console.WriteLine("This is", name) treats its first argument as a format string. Because that string has no placeholders, it prints only This is, and the name is dropped without any error. Build one string first, with + or with interpolation, and print that:
Console.WriteLine("This is " + name);
Console.WriteLine($"This is {name}");
Both lines print the same text. Remember the space inside the quotes: the + operator adds nothing between the two parts.
The exercise
The lesson asks you to write a method that introduces a person, and to call it twice. Here is the plan, step by step.
- Define the method below the
// Function goes here:comment. It ispublic, returnsvoidbecause it only prints, is namedIntroduceYourself, and takes twostringparameters:nameandlanguage. - Print the name. Join
"My name is "andname. The space at the end of the fixed text matters, because+puts nothing between the two parts. - Print the language. Do the same with
"I code in "andlanguage. - Introduce John. Under the
// Introduce Johncomment, call the method with"John"and"Python". - Introduce Mary. Under the next comment, call it again with
"Mary"and"JavaScript".
When the program runs, it prints:
My name is John
I code in Python
My name is Mary
I code in JavaScript
Notice that the two calls contain no Console.WriteLine at all. The printing lives in the method, and the calls only supply data.
Common mistakes
- Arguments in the wrong order.
IntroduceYourself("Python", "John")compiles, since both are strings, and prints a sentence that makes no sense. - Typing the value inside the method. If the body prints
"My name is John", the parameter is ignored and every call prints John. - Putting a variable name in quotes.
"name"is the word name, andnamewithout quotes is the variable. - Forgetting
void. Every method needs a return type. Usevoidwhen the method returns nothing.
Key takeaways
- A parameter is a placeholder in the method header. An argument is the value a caller passes in.
- One method with parameters replaces many near-identical blocks of code.
- Arguments are matched to parameters by position, and their types must fit.
- To change the data, change the arguments. To change the behavior, change the method body once.
- Join text into one string before passing it to
Console.WriteLine.