Problem overview
The quiz shows a function greetAndReturn with the result type String. Its first line returns "How are you doing?" a second line calls println("Hey bro!"), and the last line is the string "Bye!". A second function, mainFunction, stores the result in question and prints it. The options are four different outputs, built from the two phrases in different orders or on their own.
You answer by declaring answer as a String with the letter of the correct option.
How return ends a function
When Scala runs a return expression, the function stops at once and the value goes back to the caller. Whatever is written after the return inside the same function never runs.
That gives a way to trace the quiz:
- Call
greetAndReturn(). The first line isreturn "How are you doing?". The function ends there with that string as its result. println("Hey bro!")is skipped. It sits after thereturn, so the function has already finished when execution would reach it.questionreceives the returned string. The call inmainFunctionis an assignment, so the result is kept.println(question)prints it. This is the only thing that reaches the console:How are you doing?.
Option B shows exactly that single line, so B is the answer.
A trace of the run
Listing the events in order makes the answer concrete:
mainFunction()starts and meetsval question = greetAndReturn().- Control moves into
greetAndReturn, which immediately executes itsreturnwith"How are you doing?". - Control comes back to
mainFunctionwith that string, andquestionis bound to it. No output yet. println(question)writesHow are you doing?followed by a line break.
That is one line of output, exactly what option B shows.
Why the other options fail
- A and C both print "Hey bro!" as well, in one order or the other. That needs the
printlninsidegreetAndReturnto run, and it can't once the function has returned. - D prints only "Hey bro!", which would mean the returned value is never printed. But
mainFunctionpasses it toprintln.
The solution in Scala
val answer: String = "B"
/* Explanation:
Remember, whenever Scala 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 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.
*/
Some Scala details to notice:
val answer: String. The value can't be reassigned, which suits a quiz answer that doesn't change. The letter must be a string in double quotes.- Why the function ends with
"Bye!". A block's value is its last expression, and the compiler checks it against the declaredString. If the block ended with theprintln, its type would beUnitand the code would not compile (Found: Unit,Required: String). The final"Bye!"gives the block aStringvalue, but it never becomes the result, because thereturnhas already ended the function.
Common mistakes
- Reading code as if every line always runs. Lines after
returnin the same block are unreachable. - Thinking
returnprints. The value only reaches the console becausemainFunctionprints it. - Mixing up order. If the
println("Hey bro!")came first, "Hey bro!" would print before the return, and the output would be option A.
Key takeaways
returnends the function immediately.- Code after a
returnin the same function body doesn't run. - To print something and return something, print first and return last.
