Start learning free

LESSON 39 OF 338 · FUNCTIONS I

Creating a Function with Parameters in Go

Practice Read the full explanation

We've written this code:

func mainFunction() {
    fmt.Println("My name is Andy")
    fmt.Println("I code in Python")
    fmt.Println("I have a dog")

    fmt.Println("My name is Mike")
    fmt.Println("I code in Java")
    fmt.Println("I have a cat")

    fmt.Println("My name is Sarah")
    fmt.Println("I code in JavaScript")
    fmt.Println("I have a parrot")
}

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

The function should accept name, language, and animal, then print the three matching lines.

Assignment
Define printMessages, then call it for Andy, Mike, and Sarah.


STEP 1 OF 2
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Exercise Create Function With Parameters in Go, explained

    Problem overview

    The starting code prints three lines for each of three people: Andy, Mike and Sarah. Every group of three lines has the same shape: My name is ..., I code in ... and I have a .... Only the name, the language and the pet change.

    The task is to write the same program in a better way. You define a function called printMessages that takes name, language and animal, and prints the three matching lines. Then mainFunction calls it three times. The output must be identical to the original nine lines.

    Approach: find what repeats and what changes

    Compare the three blocks of the original code. The parts that repeat are the sentence beginnings "My name is ", "I code in " and "I have a ". The parts that change are the values: Andy, Python and dog in the first block, and different values in the others.

    The repeating parts go into the function body. The changing parts become parameters. Each call then supplies a different set of values. The nine fmt.Println lines turn into three prints in one function plus three short calls, and adding a fourth person costs one more line instead of three.

    Step by step

    1. Define the function above mainFunction. Use func, the name printMessages and three string parameters: name, language and animal. Go writes each type after its name.
    2. Print the first line. Join "My name is " and name with +. The trailing space inside the quotes matters, because + inserts nothing between its operands.
    3. Print the second and third lines the same way, with "I code in " and language, then "I have a " and animal.
    4. Call the function for Andy, inside mainFunction, with "Andy", "Python", "dog". The arguments go in the same order as the parameters.
    5. Call it for Mike with "Mike", "Java", "cat", and for Sarah with "Sarah", "JavaScript", "parrot".

    The solution in Go

    // Define & implement the function printMessages here:
    func printMessages(name string, language string, animal string) {
        fmt.Println("My name is " + name)
        fmt.Println("I code in " + language)
        fmt.Println("I have a " + animal)
    }
    
    
    func mainFunction() {
        // Call printMessages 3 times here:
        printMessages("Andy", "Python", "dog")
        printMessages("Mike", "Java", "cat")
        printMessages("Sarah", "JavaScript", "parrot")
    }
    

    Go details worth knowing about this solution:

    • Grouped parameter types. When consecutive parameters have the same type, you can write the type once: func printMessages(name, language, animal string). It is the same function. The longer form in the solution is just as valid.
    • Positional matching. Go has no named arguments. The first argument fills name, the second language and the third animal. Because all three are strings, a wrong order compiles and prints wrong text, such as My name is dog.
    • Exact argument count. A call with two arguments or four is a compile error. There are no default values.
    • Return type. The function declares no result, so it returns nothing. It prints, and the caller just calls it as a statement.
    • Copies. Each call gets its own copy of the strings, so the three calls do not affect each other.

    Edge cases and common mistakes

    • Missing space. "My name is" + name prints My name isAndy. Keep the space at the end of each fixed piece.
    • Quoting the parameter. "name" in quotes is the word name. Without quotes, name is the parameter's value.
    • Wrong order of calls. The output has to list Andy, Mike and Sarah in that order, so the calls must come in that order too.
    • Printing the three lines of one person together. Each call must print all three lines before the next call starts. Putting all names first and then all languages would give a different output.
    • Defining the function inside mainFunction. Go does not allow a named func declaration inside another function. Define it at the top level, above mainFunction.

    Testing

    Run the program and compare it with the original. The first three lines should be:

    My name is Andy
    I code in Python
    I have a dog
    

    The next six lines follow the same pattern for Mike (Java, cat) and Sarah (JavaScript, parrot).

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free