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.
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 line
fmt.Println("Hey bro!")comes after thereturn. Execution never reaches it, soHey bro!is never printed. - 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.
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
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.