Start learning free

LESSON 47 OF 338 · FUNCTIONS I

Return in Go: Quiz IV on Code After a Return

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

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

func mainFunction() {
    question := greetAndReturn()
    fmt.Println(question)
}

Important Note:

Do not use an actual code editor to get the answer. It would defeat the purpose of the quiz.

Pick your answer

Next lesson

GO · FUNCTIONS I

Quiz Return 7 in Go, explained

What the quiz asks

The quiz shows a function, greetAndReturn, that returns a string, and a second function, mainFunction, that calls it and prints the result. You choose which of four outputs the code produces. You answer by assigning the letter to a variable called answer.

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

func mainFunction() {
    question := greetAndReturn()
    fmt.Println(question)
}

The options differ in which lines appear and in what order: Hey bro! then How are you doing?, only How are you doing?, the two lines in reverse order, or only Hey bro!.

How to think about it

Trace the calls in order, the way a computer does.

  1. mainFunction starts and reaches question := greetAndReturn(). Go has to finish the call before it can store anything in question, so it enters greetAndReturn.
  2. The first statement in greetAndReturn is return "How are you doing?". A return ends the function on the spot and hands its value to the caller.
  3. The line fmt.Println("Hey bro!") comes after the return. Execution never reaches it, so Hey bro! is never printed.
  4. Back in mainFunction, question now holds How are you doing?, and fmt.Println(question) prints it.

The only line that reaches the screen is How are you doing?, which is option B.

Two points are easy to mix up. Returning a value is different from printing it: the string travels back to the caller, and nothing appears until some code prints it. And the order of statements inside a function matters, because a return cuts off everything below it.

The solution in Go

var answer string = "B"

/* Explanation:

Remember, whenever Go hits a "return" statement inside a function, the value is being returned from the function
and the function call ends immediately after that. Even if there are more lines of code coming after the "return", they will not be executed.

In our case, when we call greetAndReturn(), the string "How are you doing?" is being returned and the function call ends.
Thus, the fmt.Println("Hey bro!") will never get executed.

However, variable greeting will be assigned the returned string "How are you doing?", which will be printed to the console.
*/

The declaration var answer string = "B" names the type explicitly. In Go you could also write answer := "B" and the compiler would infer string, but the quiz asks for the var form. The letter goes in double quotes because it is a string, not a single character.

What a real Go compiler says

The quiz asks about the idea of return, and answer B follows from it. If you paste exactly this function into a real Go program, though, the compiler does not let it through. A function that declares a result type must end with a terminating statement, such as a return, and here the last statement is the print. The build stops with this message:

./prog.go:8:1: missing return

Go does not complain about the unreachable print by itself. It complains because, as far as the compiler can tell, the function can finish without returning anything. The fix is to move the print above the return:

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

With that order, running the same mainFunction prints Hey bro! first and How are you doing? second, which is option A. Putting the print before the return is the only way to see both lines.

Common mistakes

  • Thinking every line in a function runs. Lines after a return on the same path are skipped.
  • Expecting the return to print. A returned value is only handed back. mainFunction prints it with fmt.Println(question).
  • Choosing the reversed order (C). That would need the print to run after the return value was produced, which a return never allows.
  • Choosing D. The print inside greetAndReturn is skipped, and the returned text does get printed by the caller, so Hey bro! never appears alone.
  • Writing the letter without quotes. var answer string = B looks for a variable named B and fails to compile.

Key takeaways

  • A return ends the function immediately and passes its value to the caller.
  • Statements placed after a return on the same path never run.
  • Put anything that must happen, such as a print, before the return.
  • A Go function with a result type must end in a terminating statement such as 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