Start learning free

LESSON 35 OF 347 · FUNCTIONS I

Method Parameters and Arguments in C#

Lesson Read the full explanation

So far, the functions we've created execute a fixed task without context / inputs. But most often we want our functions to produce different outcomes depending on the context behind the function call.

For example, it would be nice if sayHello() would print a personalized message such as "Hello humans" or "Hello cats" or "Hello dogs", depending on the audience instead of "Hello world" no matter what.


Function parameters:

Function parameters allow functions to accept input(s) and perform a task using the input(s). We use parameters as placeholders for information that will be passed to the function when it is called.

When a function is defined, its parameters are specified between the parentheses that follow the function name.

Here is our function with one String parameter, audience:

void sayHello(string audience) {
  Console.WriteLine("Hello " + audience);
}

Calling with arguments:

Then we can call sayHello() and specify the values in the parentheses that follow the function name. The values that are passed to the function when it is called are called arguments.

// Function declaration:
void sayHello(string audience) {
	Console.WriteLine("Hello " + audience);
}

// Function call:
sayHello("humans"); // Output: Hello humans

We have passed one string argument: "humans". Inside the function audience will equal string "humans" and acts just like a regular variable.


Multiple parameters:

A function can have as many parameters as it needs. Here is a function with two parameters:

void sayHello(string name, int age) {
  Console.WriteLine(name + ", aged " + age);
}

// Let's call it:
sayHello("John", 30); // Output: John, aged 30
sayHello("Mary", 26); // Output: Mary, aged 26

Notice that the order in which arguments are passed and assigned follows the order that the parameters are declared.


Variables as arguments:

Variables can also be passed as arguments for function calls:

void sayHello(string name, int age) {
  Console.WriteLine(name + ", aged " + age);
}

string name = "Andy";
int age = 28;

sayHello(name, age); // Output: Andy, aged 28

sayHello("Mary", age); // Output: Mary, aged 28

Assignment
Follow the Coding Tutorial and let's write some functions.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 5
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Function Parameters And Arguments in C#, explained

    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

    1. Define the method with a parameter. Above MainFunction, write public void WelcomeUser(string name) and a pair of braces. It's void because it only prints.
    2. 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 is Welcome, Andy!.
    3. Call it with a literal. In MainFunction, write WelcomeUser("Andy");. The parameter name receives "Andy".
    4. Create a variable. Still in MainFunction, write string name = "John";. This name is a local variable of MainFunction. It's not the parameter of WelcomeUser, even though the names match, because each lives in its own method.
    5. 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, and WelcomeUser(string name) is right.
    • Passing the wrong type. A string parameter won't accept an int argument, 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," + name would print Welcome,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 an int is converted to text in the join.
    • The method works on a copy of the value, so it doesn't change the caller's variable.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free