Problem overview
This quiz shows a function, greetAndReturn, that has a println call after its return, and a second function, mainFunction, that calls it and prints the result. You have to decide what the console shows, choosing among four options: Hey bro! then How are you doing? (A), only How are you doing? (B), the same two lines in the opposite order (C), or only Hey bro! (D). You then record the letter in a variable named answer.
The way to solve it is to trace the program by hand, one statement at a time, exactly as the computer would.
Approach: trace the program
Here is the code from the quiz:
fun greetAndReturn(): String {
return "How are you doing?"
println("Hey bro!")
}
fun mainFunction() {
val question: String = greetAndReturn()
println(question)
}
Execution starts in mainFunction, and the function bodies are not read from top to bottom as a whole. Only the statements that actually run count.
mainFunctionreachesval question: String = greetAndReturn(). To get the value forquestion, it must first callgreetAndReturn.- Inside
greetAndReturn, the first statement isreturn "How are you doing?". Areturnends the function on the spot and hands the value to the caller. - The line
println("Hey bro!")sits below thereturn, so it is never reached. The code still compiles and runs; the line is simply skipped. - Back in
mainFunction,questionnow holds"How are you doing?". - The last line,
println(question), prints that text.
The console therefore shows one line:
How are you doing?
That is option B. Option A and option C both contain Hey bro!, which would require the println after the return to run. Option D prints only the greeting that never runs.
Step by step: submitting the answer
- Declare the variable with
val. The answer is chosen once and never changes, so a read-only variable is the right fit. - Give it the type
String. The letter is text, so it goes in double quotes:"B", notBand not'B'. - Use the exact name
answer. The lesson's tests look for that name.
The solution in Kotlin
val answer: String = "B"
/* Explanation:
Remember, whenever Kotlin 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.
*/
A few details about these lines:
val answer: String = "B"is a top-level declaration. It needs no function around it.- The type annotation is optional here, since Kotlin infers
Stringfrom the literal. The lesson spells it out for practice. - Everything between
/*and*/is a block comment. The compiler ignores it, however many lines it spans.
Why this matters
Code after a return never runs. That is not a quirk of this example; it is how every function works. A function that does work and also returns a value has to do the work first and return last, as in this reordered version:
fun greetAndReturn(): String {
println("Hey bro!")
return "How are you doing?"
}
With that order, the console shows Hey bro! and then How are you doing?, which is option A.
A return inside an if behaves the same way, and that is useful: when the condition holds, the function ends there, and the statements below are skipped for that call only.
Common mistakes
- Reading top to bottom and printing everything. The
printlnline is visible in the code but is never executed. - Thinking the function returns a value and also continues. A function call ends at its first executed
return. - Writing the letter without quotes.
val answer: String = Bmakes Kotlin look for a variable namedB, and the code fails to compile. - Running the code to find the answer. The quiz is for practising the tracing by hand, which is the skill you need when debugging.