Start learning free

LESSON 34 OF 338 · FUNCTIONS I

Create a Function in Go: Print Messages Three Times

Practice Read the full explanation

We've written this code:

func mainFunction() {
    fmt.Println("Hello AlgoCademy!")
    fmt.Println("Coding is amazing!")
    fmt.Println("Functions are so useful!")

    fmt.Println("Hello AlgoCademy!")
    fmt.Println("Coding is amazing!")
    fmt.Println("Functions are so useful!")

    fmt.Println("Hello AlgoCademy!")
    fmt.Println("Coding is amazing!")
    fmt.Println("Functions are so useful!")
}

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

The function should print:

Hello AlgoCademy!
Coding is amazing!
Functions are so useful!

Assignment
Define printMessages, then call it three times from mainFunction.


STEP 1 OF 2
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Exercise Create Function in Go, explained

    Problem overview

    The starting program prints the same three lines three times by writing out nine fmt.Println calls. The task is to say the same thing in a better way: define a function printMessages that prints the three lines once, and call it three times from mainFunction.

    The output must be exactly:

    Hello AlgoCademy!
    Coding is amazing!
    Functions are so useful!
    Hello AlgoCademy!
    Coding is amazing!
    Functions are so useful!
    Hello AlgoCademy!
    Coding is amazing!
    Functions are so useful!
    

    Approach: find the repeated block

    Read the original code and look for a group of statements that appears more than once. Here the same three lines repeat back to back. That group is the body of the new function, and each repetition becomes a single call.

    The result is shorter, and it changes in one place: edit one message in printMessages and all three repetitions follow.

    Step by step

    1. Define printMessages above mainFunction. It takes no parameters, so the parentheses are empty. It returns nothing, so no return type follows them.
    2. Put one copy of the three prints in its body. Keep the order and the text exactly as the task shows.
    3. Call it from mainFunction. Write the name followed by parentheses, once for each repetition.
    4. Count the calls. Three calls mean three runs of the body, so nine lines of output.

    The solution in Go

    // Define & implement the function printMessages here:
    func printMessages() {
        fmt.Println("Hello AlgoCademy!")
        fmt.Println("Coding is amazing!")
        fmt.Println("Functions are so useful!")
    }
    func mainFunction() {
        // Call printMessages 3 times here:
        printMessages()
        printMessages()
        printMessages()
    }
    

    Notes on the Go involved:

    • No return type. A function that only prints needs nothing after its parentheses.
    • The parentheses make the call. A line that holds just printMessages is rejected by the compiler as a value that is not used. It does not run the function.
    • Each Println ends with a newline, so no \n is needed inside the strings.
    • Order of the definitions. Go would accept printMessages below mainFunction as well. Writing helpers above the code that uses them is a readability habit.
    • A loop is another tool. Later you will repeat a call with for instead of writing it three times. This lesson asks for three separate calls.

    Edge cases and common mistakes

    • Two calls or four. The expected output has nine lines, which is exactly three runs of the body.
    • A different order inside the body. The three lines must appear in the order shown, since each call repeats them in that order.
    • A typo in a message. The capital letters and the exclamation marks are part of the expected output. Hello Algocademy! is a different string.
    • Keeping some of the old Println lines in mainFunction. They would add extra output. The nine original lines are replaced by the three calls.
    • Misspelling the function name. Calling printMessage() gives an undefined error, because Go looks for exactly the name you wrote.

    Why this is better than nine lines

    Count the cost of a change. Say the second message must become Coding is fun!. In the original program you would edit three separate lines and could easily miss one. With printMessages you edit one line, and every call prints the new text. The same goes for adding a message or fixing a typo.

    The function also gives the repeated block a name that describes it. A reader of mainFunction sees three calls to printMessages and understands the program at a glance, without comparing nine lines to see that they come in three identical groups.

    Testing

    Run the code and compare the output with the nine lines at the top. If you change the function to print a fourth line, all three blocks of output should grow by that line. That confirms the change lives in one place.

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