Why methods need inputs
The methods so far do one fixed job. SayHello() prints the same greeting every time. Most useful methods need to vary what they do depending on the situation, such as greeting a particular person or audience. You give them that information through parameters.
Parameters: the placeholders
A parameter is a variable declared in the parentheses of a method's header. It stands in for a value that will be supplied later, each time the method is called. In C# every parameter needs a type and a name:
void SayHello(string audience) {
Console.WriteLine("Hello " + audience);
}
Here audience is a parameter of type string. Inside the body it behaves like an ordinary variable that already has a value.
Arguments: the actual values
The values you pass in a call are arguments. They go inside the parentheses after the method's name:
SayHello("humans"); // Hello humans
SayHello("cats"); // Hello cats
In the first call, the argument "humans" is copied into the parameter audience, so the body prints Hello humans. The simplest way to remember the two words: the parameter is the name in the definition, and the argument is the value in the call.
Multiple parameters
A method can have as many parameters as it needs, separated by commas. Each has its own type:
void SayHello(string name, int age) {
Console.WriteLine(name + ", aged " + age);
}
SayHello("John", 30); // John, aged 30
SayHello("Mary", 26); // Mary, aged 26
Arguments are matched to parameters by position, so the first argument goes to the first parameter and so on. Putting them in the wrong order, SayHello(30, "John"), is a compile error because the types no longer match. If the types had happened to match, you'd get no error but the values would be swapped, so check the order.
The number of arguments must match too. Calling SayHello() with nothing, when the method requires a string, doesn't compile.
Variables as arguments
An argument can be any expression with the right type, including a variable. The method receives the variable's current value:
string name = "Andy";
int age = 28;
SayHello(name, age); // Andy, aged 28
SayHello("Mary", age); // Mary, aged 28
Because the value is copied, changing a parameter inside a method doesn't change the caller's variable, for simple types like int and string. If a method adds 1 to its int parameter, the variable you passed keeps its old value.
The exercise, step by step
- Define the method with a parameter. Above
MainFunction, writepublic void WelcomeUser(string name)and a pair of braces. It'svoidbecause it only prints. - Print the greeting. In the body, print
"Welcome, " + name + "!". The+joins the strings, and the space after the comma belongs in the first piece. With"Andy", the output isWelcome, Andy!. - Call it with a literal. In
MainFunction, writeWelcomeUser("Andy");. The parameternamereceives"Andy". - Create a variable. Still in
MainFunction, writestring name = "John";. Thisnameis a local variable ofMainFunction. It's not the parameter ofWelcomeUser, even though the names match, because each lives in its own method. - Call it with the variable. Write
WelcomeUser(name);. The method gets the value"John".
public void WelcomeUser(string name) {
Console.WriteLine("Welcome, " + name + "!");
}
public void MainFunction() {
WelcomeUser("Andy");
string name = "John";
WelcomeUser(name);
}
Running MainFunction prints:
Welcome, Andy!
Welcome, John!
Common mistakes
- Leaving out the type. C# requires it:
WelcomeUser(name)in the definition is an error, andWelcomeUser(string name)is right. - Passing the wrong type. A
stringparameter won't accept anintargument, or the other way round. - Declaring a local variable with the parameter's name inside the same method. C# reports an error, since the name is already taken in that scope.
- Forgetting the spaces and punctuation in the greeting.
"Welcome," + namewould printWelcome,Andy.
Key takeaways
- Parameters are named, typed placeholders in the definition. Arguments are the values in the call.
- Arguments match parameters by position, number and type.
- Strings can be joined with
+, and anintis converted to text in the join. - The method works on a copy of the value, so it doesn't change the caller's variable.