Start learning free

LESSON 38 OF 347 · FUNCTIONS I

Why Parameters and Arguments Matter in C#

Lesson Read the full explanation

Now that we've learned about function parameters and arguments, let's see why they are so important in our prgrammers' life:

Consider this: Three students named Andy, Mike and Sally made a mistake. And so their teacher asked each of them to write an apology on the whiteboard.

We can put the apologies in a program like this:

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");

That's decent. But what if we want to change the students' names?

We would have to go through the whole program and change each individual name with the new one.

And if we dealt with 10 students instead of 3 and if the apology text consisted of more than 3 lines, this would quickly get out of hand.


The story with functions:

This is where functions with parameters shine. We can write a function that takes in a student's name as argument and prints the specific aplogy for that student.

Then, we'll just call the function for each individual student:

void printMessage(string name) {
	Console.WriteLine("This is", name);
	Console.WriteLine(name, "made a mistake");
	Console.WriteLine(name, "suffers the consequences");
}

printMessage("Andy");
printMessage("Mike");
printMessage("Sally");

Changing the story:

Need to change the students' names? We only change the arguments in the function calls.

Need to change the aplogy text a little bit? We only change it inside the function instead of changing it in the whole program.

void printMessage(string name) {
	Console.WriteLine("This is the apology of", name);
	Console.WriteLine(name, "made a mistake and so", name, "suffers the consequences");
}

printMessage("Andrei");
printMessage("Paul");
printMessage("Mary");

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

    Why Parameters And Arguments in C#, explained

    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 string is expected, is a compile-time error.
    • Arguments match by position. In IntroduceYourself("John", "Python"), the first argument goes to name and the second to language. 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 as name.

    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.

    1. Define the method below the // Function goes here: comment. It is public, returns void because it only prints, is named IntroduceYourself, and takes two string parameters: name and language.
    2. Print the name. Join "My name is " and name. The space at the end of the fixed text matters, because + puts nothing between the two parts.
    3. Print the language. Do the same with "I code in " and language.
    4. Introduce John. Under the // Introduce John comment, call the method with "John" and "Python".
    5. 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, and name without quotes is the variable.
    • Forgetting void. Every method needs a return type. Use void when 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.
    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