Start learning free

LESSON 48 OF 348 · FUNCTIONS I

Quiz: Code After return in Kotlin, Explained

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?

fun greetAndReturn(): String {
    return "How are you doing?"
    println("Hey bro!")
}

fun mainFunction() {
    val question: String = greetAndReturn()
    println(question)
}

Important Note:

Do not use an actual code editor to get the answer! It would defy the whole purpose of the quiz!

Pick your answer

Next lesson

KOTLIN · FUNCTIONS I

Quiz Return 7 in Kotlin, explained

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.

  1. mainFunction reaches val question: String = greetAndReturn(). To get the value for question, it must first call greetAndReturn.
  2. Inside greetAndReturn, the first statement is return "How are you doing?". A return ends the function on the spot and hands the value to the caller.
  3. The line println("Hey bro!") sits below the return, so it is never reached. The code still compiles and runs; the line is simply skipped.
  4. Back in mainFunction, question now holds "How are you doing?".
  5. 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

  1. Declare the variable with val. The answer is chosen once and never changes, so a read-only variable is the right fit.
  2. Give it the type String. The letter is text, so it goes in double quotes: "B", not B and not 'B'.
  3. 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 String from 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 println line 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 = B makes Kotlin look for a variable named B, 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.
Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free