Start learning free

LESSON 47 OF 404 · 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!")
    return "Bye!"
}

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!")
    return "Bye!"
}

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 lines fmt.Println("Hey bro!") and return "Bye!" come after the first return. Execution never reaches them, so Hey bro! is never printed and Bye! is never returned.
  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.

Why the function ends with a second return

Go requires a function that declares a result type to end with a terminating statement, such as a return. Without the last line, the function would end with the print and the build would stop with missing return. The final return "Bye!" satisfies that rule but never runs, because the first return has already ended the function. Go accepts unreachable statements like these; it only checks that the function cannot finish without returning.

To see both lines, move the print above the first return:

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

With that order, the same mainFunction prints Hey bro! first and How are you doing? second, which is option A.

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