Start learning free

LESSON 30 OF 338 · FUNCTIONS I

Calling a Function in Go: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to define and call a function that prints the messages Hey coder! and Welcome back!.

When the code is fixed, it should print:

Hey coder!
Welcome back!

In Go, a function call uses the function name followed by parentheses:

welcomeUser()

Assignment:

Your task is to fix the code so no errors are produced and it prints the desired output.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Buggy Code Call Before Definition in Go, explained

    What the code is supposed to do

    The program defines a function welcomeUser and calls it from mainFunction. It should print two lines, in this order:

    Hey coder!
    Welcome back!
    

    Here is the code you start with:

    func welcomeUser() {
        fmt.Println("Welcome back!")
        fmt.Println("Hey coder!")
    }
    func mainFunction() {
        welcomeUser()
    }
    

    It compiles and runs, but the two messages appear the wrong way round: Welcome back! first, then Hey coder!.

    What goes wrong

    There is no compile error here, and the function is called correctly. mainFunction calls welcomeUser() exactly once, with the name followed by parentheses. The mistake is in the body.

    Go runs the statements of a function body from top to bottom, in the order they are written. The first fmt.Println prints Welcome back! and the second prints Hey coder!. The output therefore follows the order of the lines in the source, not the order the messages appear in your head.

    Order of statements and order of declarations

    These are two different ideas, and the lesson's title hints at the second one.

    • Statements inside a function run in the order they are written. Swapping two lines changes the output.
    • Declarations at package level can come in any order. A function may be called from code placed above its definition, because Go sees all package-level declarations together. Here mainFunction could come first and welcomeUser after it, and the program would behave the same.

    So the fix is not to move the functions around. It is to reorder the two print lines inside the one function.

    Fixing it step by step

    1. Find where the order is decided. welcomeUser() is called once, so the order of the output comes from the body of welcomeUser.
    2. Compare the lines with the expected output. The first line printed should be Hey coder!, but the first Println in the body prints Welcome back!.
    3. Swap the two Println lines. Keep the strings exactly as they are; only their positions change.
    4. Leave mainFunction alone. It already calls the function once.

    The fixed code in Go

    func welcomeUser() {
        fmt.Println("Hey coder!")
        fmt.Println("Welcome back!")
    }
    func mainFunction() {
        welcomeUser()
    }
    /* Explanation:
    Remember, the statements inside a function are executed in the exact order we write them.
    That's why "Welcome back!" gets printed before "Hey coder!" in the console.
    We have to swap the "fmt.Println()" statements' order inside our function.
    */
    

    The comment block at the end is only an explanation, and Go ignores everything between /* and */. A few details are worth knowing:

    • Each Println ends its own line. That is why the two messages land on separate lines without any \n in the strings.
    • The text must match exactly. Capitals, the space and the exclamation mark are all part of the expected output.
    • Comments in Go start with // for one line or sit between /* and */ for several lines.

    Running the fixed function prints:

    Hey coder!
    Welcome back!
    

    Common mistakes

    • Editing mainFunction. Adding a second call would print the pair of messages twice, and neither change touches the order inside the pair.
    • Changing the strings instead of the order. Typing Hey coder! into the first line and Welcome back! into the second works, but the swap is simpler and keeps the text untouched.
    • Moving the function definition. It makes no difference, since Go does not care which function is declared first.
    • Dropping the parentheses in the call. welcomeUser without () is rejected by the compiler as a value that is not used.

    Checking your fix

    Run the code in the editor above. The output should be exactly the two lines shown earlier, Hey coder! followed by Welcome back!, each on its own line.

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