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.
mainFunctionstarts and reachesquestion := greetAndReturn(). Go has to finish the call before it can store anything inquestion, so it entersgreetAndReturn.- The first statement in
greetAndReturnisreturn "How are you doing?". Areturnends the function on the spot and hands its value to the caller. - The lines
fmt.Println("Hey bro!")andreturn "Bye!"come after the firstreturn. Execution never reaches them, soHey bro!is never printed andBye!is never returned. - Back in
mainFunction,questionnow holdsHow are you doing?, andfmt.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
returnon the same path are skipped. - Expecting the return to print. A returned value is only handed back.
mainFunctionprints it withfmt.Println(question). - Choosing the reversed order (C). That would need the print to run after the return value was produced, which a
returnnever allows. - Choosing D. The print inside
greetAndReturnis skipped, and the returned text does get printed by the caller, soHey bro!never appears alone. - Writing the letter without quotes.
var answer string = Blooks for a variable namedBand fails to compile.
Key takeaways
- A
returnends the function immediately and passes its value to the caller. - Statements placed after a
returnon 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.
