Start learning free

LESSON 48 OF 338 · FUNCTIONS I

Return in Go: Fixing the Buggy Code IV

Practice Read the full explanation

We've written a program and expected it to print:

Hey bro!
How are you doing?

but we get a different output.

A return statement stops the function immediately, so any print statement that should run must happen before the return.

Assignment:

Fix the function so it prints Hey bro! first, then returns How are you doing?.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Buggy Code 6 in Go, explained

    What the code should do

    The program has two functions. greetAndReturn should print Hey bro! and then return the string How are you doing?. mainFunction calls it, stores the returned string in question and prints it. The expected output is two lines:

    Hey bro!
    How are you doing?
    

    This is the code you start with:

    func greetAndReturn() string {
        return "How are you doing?"
        fmt.Println("Hey bro!")
    }
    
    func mainFunction() {
        question := greetAndReturn()
        fmt.Println(question)
    }
    

    What goes wrong

    Go runs the statements of a function from top to bottom. The first statement in greetAndReturn is the return, and a return ends the function right away. The fmt.Println("Hey bro!") below it can never run.

    The Go compiler also objects to the shape of the function. A function with a result type, here string, must end with a terminating statement, and a return is the usual one. In this version the last statement is the print, so the compiler concludes that the function could reach its closing brace without returning a value, and stops with:

    missing return
    

    Both symptoms have the same cause: the two lines are in the wrong order. Printing the greeting has to happen before the function hands control back.

    Fixing it step by step

    1. Find the line that must run first. The greeting is printed by fmt.Println("Hey bro!"), and the expected output shows it first.
    2. Move it above the return. The print becomes the first statement of greetAndReturn, so it runs every time the function is called.
    3. End with the return. return "How are you doing?" is now the last statement. This satisfies the compiler's rule and also hands the string back to the caller.
    4. Leave mainFunction alone. It already calls the function and prints what comes back, which gives the second line of output.

    The fixed code in Go

    func greetAndReturn() string {
        fmt.Println("Hey bro!")
        return "How are you doing?"
    }
    
    func mainFunction() {
        question := greetAndReturn()
        fmt.Println(question)
    }
    

    When mainFunction runs, the order of events is:

    1. The call greetAndReturn() begins, and it prints Hey bro!.
    2. It returns How are you doing?, and the call ends.
    3. The returned string is stored in question.
    4. fmt.Println(question) prints How are you doing?.

    Both messages appear, but they come from different places. The first is printed inside greetAndReturn. The second is printed by mainFunction, after the value has travelled back. If you had put fmt.Println of the second string inside greetAndReturn as well, it would print twice.

    Why the order matters

    Think of return as the last thing a function does. Before it, a function can do any amount of work: print, calculate, call other functions. The return then sends the result back and finishes the call. This is why a function that both prints and returns, like greetAndReturn, always has its printing first.

    The same idea applies to larger functions. If an if block contains a return, the lines after that return inside the block are skipped, and the lines after the whole if block run only when the block was not entered.

    Common mistakes

    • Treating return as a print. A return only passes a value to the caller. Something has to print it.
    • Putting code after a return. Anything below a return on the same path is dead code. Go accepts that on its own, but a function with a result still needs a terminating statement at the end.
    • Deleting the return to make the error go away. The function would then have no value to give back, and question := greetAndReturn() would have nothing to store.
    • Swapping to a different order of two prints. Any arrangement that keeps a print below the return fails, so the print has to go above it.

    Checking your fix

    Call mainFunction() from a main function and run the program. The fixed version prints Hey bro! on the first line and How are you doing? on the second. The original does not build at all, because of the missing return.

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