Start learning free

LESSON 39 OF 347 · FUNCTIONS I

Create a Method with Parameters in C#: PrintMessages

Practice Read the full explanation

We've written this code:

void mainFunction() {
	Console.WriteLine("My name is Andy");
	Console.WriteLine("I code in Python");
	Console.WriteLine("I have a dog");

	Console.WriteLine("My name is Mike");
	Console.WriteLine("I code in Java");
	Console.WriteLine("I have a cat");

	Console.WriteLine("My name is Sarah");
	Console.WriteLine("I code in JavaScript");
	Console.WriteLine("I have a parrot");
}

Write the same code in a better way by creating and calling a function with parameters printMessages()

STEP 1 OF 2
    Ctrl + Enter to check

    C# · FUNCTIONS I

    Exercise Create Function With Parameters in C#, explained

    Problem overview

    The starting program prints three short introductions, one each for Andy, Mike and Sarah. Every introduction has the same three-line shape: a name, a programming language and a pet. Only the three values change from one person to the next. The task is to write a method, PrintMessages, that prints one introduction from three values, and then to call it three times.

    The output must stay exactly the same as the long version:

    My name is Andy
    I code in Python
    I have a dog
    My name is Mike
    I code in Java
    I have a cat
    My name is Sarah
    I code in JavaScript
    I have a parrot
    

    Approach

    The nine Console.WriteLine calls are three copies of one pattern. Whenever you notice the same lines repeated with different values, turn the lines into a method and turn the values into parameters. The method holds the shared structure, and each call supplies the values that differ.

    Three values differ, so the method takes three parameters: name, language and animal. All three are text, so all three are string. The method prints and gives nothing back, so its return type is void. That gives the header:

    public void PrintMessages(string name, string language, string animal)
    

    A good way to find the parameters is to compare the repeated blocks side by side and underline what differs. Here you would underline Andy, Python and dog in the first block, and Mike, Java and cat in the second. Each underlined spot becomes a parameter, and everything left over is the fixed text of the method body.

    Step by step

    1. Write the header. Put the method above MainFunction, under the comment that marks the spot. The parameter order is a choice you make once and then follow in every call.
    2. Move the printing into the body. Keep three Console.WriteLine lines, one for each sentence. In each, join the fixed text and the matching parameter with +.
    3. Mind the spaces. The fixed texts end with a space, as in "My name is ". The + operator adds nothing between its two sides, so without that space you would get My name isAndy.
    4. Call the method three times. In MainFunction, write one call per person. Pass the arguments in the same order as the parameters: name, language, animal.

    The solution in C#

    // Define and implement the function PrintMessages here:
    public void PrintMessages(string name, string language, string animal) {
        Console.WriteLine("My name is " + name);
        Console.WriteLine("I code in " + language);
        Console.WriteLine("I have a " + animal);
    }
    
    
    public void MainFunction() {
        // Call PrintMessages 3 times here:
        PrintMessages("Andy", "Python", "dog");
        PrintMessages("Mike", "Java", "cat");
        PrintMessages("Sarah", "JavaScript", "parrot");
    }
    

    A few C# details about this code:

    • Every parameter has its own type. C# does not allow (name, language, animal) with a single shared type. Write string before each name.
    • Arguments match by position. In the first call, "Andy" goes to name, "Python" to language and "dog" to animal. The compiler cannot tell if you swap two strings, so the order is your responsibility.
    • Each call starts with fresh parameters. The values from the Andy call are gone by the time the Mike call starts. The method keeps no memory between calls.
    • The names are your own. A parameter is only a name for a value inside the method. Rename animal to pet everywhere in the body and the program behaves the same.
    • The method is void. It has no return statement, because it displays text and does not give a value to its caller.

    Why this is better

    The new version has three parts, and each part has one job. The method says how an introduction looks, and the calls say who is introduced. If you later want a fourth person, you add one line. If you want to change the wording, for example to "I love " in place of "I code in ", you edit one line, instead of one line per person.

    You can also write the sentences with string interpolation, for example $"My name is {name}". It produces identical text. This lesson uses +, which is the form to practice first.

    Edge cases and common mistakes

    • Missing space before the value. "I code in" followed by + language prints I code inPython.
    • Arguments in the wrong order. PrintMessages("Python", "Andy", "dog") compiles and prints nonsense. Check the order against the header.
    • Too few or too many arguments. A call with two arguments to a three-parameter method is a compile error.
    • Quoting a parameter name. "name" in quotes is the word, not the variable.
    • Calling the method before defining it. In C#, the order of methods inside a class does not matter, so the method could even sit below MainFunction. This lesson places it above to keep the reading order natural.
    • Extra output. Leave out any additional WriteLine, including empty ones between introductions. The expected output has no blank lines.

    Testing

    Try a call with different values, for example PrintMessages("Ana", "C#", "fish"). It should print:

    My name is Ana
    I code in C#
    I have a fish
    

    If a value you never wrote into the method shows up in the output, the parameters are doing their job.

    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