Start learning free

LESSON 44 OF 671 · FUNCTIONS I

Fixing Print Instead of Return in Go

Practice Read the full explanation

We've written a program and expected it to print Hey, Andy, but the function prints instead of returning the greeting, so the Go compiler rejects it with missing return.

The caller already stores the returned value in greeting and prints it afterwards, so the function should use return.

Assignment:

Fix getGreeting so it returns "Hey, " + name.


STEP 1 OF 1
    Ctrl + Enter to check

    GO · FUNCTIONS I

    Buggy Code Print Instead Of Return in Go, explained

    What the code is supposed to do

    The function getGreeting should build a greeting for name and give it back to the caller. mainFunction calls it with "Andy", stores the result in greeting and prints that variable. The program should print Hey, Andy once.

    This is the starting code:

    func getGreeting(name string) string {
        fmt.Println("Hey, " + name)
    }
    // Do not change the code below:
    func mainFunction() {
        greeting := getGreeting("Andy")
        fmt.Println(greeting)
    }
    

    The compiler rejects it with missing return.

    What goes wrong

    The header ends with string, which promises that getGreeting gives a string back. The body doesn't keep that promise. It builds "Hey, " + name and passes it to fmt.Println, which shows it on the screen. Showing a value and returning it are two different things. After the print, the function reaches its closing brace with nothing returned.

    A Go function with a result type must return on every path through its body. Here no path does, so the program is rejected before it can run.

    Imagine that the code did compile. The greeting would be printed inside the function, and then mainFunction would print the returned value as well. The caller already does the printing, so the function has to hand the text over instead.

    Fixing it step by step

    1. Keep the header. One string parameter and a string result are what the caller expects.
    2. Replace the print with return. The expression "Hey, " + name stays the same. Only the call around it changes.
    3. Leave the caller alone. It stores the returned value in greeting and prints it, and that is the only printing the program needs.

    The fixed code in Go

    func getGreeting(name string) string {
        return "Hey, " + name
    }
    // Do not change the code below:
    func mainFunction() {
        greeting := getGreeting("Andy")
        fmt.Println(greeting)
    }
    

    Details worth knowing:

    • The space after the comma is part of the string. "Hey, " ends with a space, because + inserts nothing between the pieces. Without it the output would be Hey,Andy.
    • No fmt needed in the function now. The package is still used by mainFunction, so the import stays valid.
    • Returning keeps the function reusable. A function that returns text can feed a print, a file, a longer message or a test. A function that only prints can do just one thing.
    • Printing and returning together. If you wanted both, you would print and then also write return. The program would then show the greeting twice, which this task does not want.

    Common mistakes

    • Adding return but keeping the print. The code compiles, but Hey, Andy appears twice, once from getGreeting and once from mainFunction.
    • Removing the result type. Without string in the header, the line greeting := getGreeting("Andy") in the unchangeable code fails to compile.
    • Returning a different value. return name prints Andy, not Hey, Andy.
    • Changing the caller. The task marks it as unchangeable, and it was never the problem.

    Reading the error

    The message missing return points at the closing brace of getGreeting. That position is a clue: the compiler walked through the whole body, reached the end, and found that the function can finish without handing back a string. Whenever you see this message, look for a path through the function that ends without a return, such as a body that only prints, or an if branch that returns while the rest of the function does not.

    Checking your fix

    Run the code in the editor above. A correct fix compiles and prints exactly one line, Hey, Andy. If you see the line twice, a fmt.Println is still left inside getGreeting.

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