Start learning free

LESSON 48 OF 414 · FUNCTIONS I

Return in Scala: Quiz on Code After a Return

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?

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

def mainFunction(): Unit = {
    val question = 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

SCALA · FUNCTIONS I

Quiz Return 7 in Scala, explained

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:

  1. Call greetAndReturn(). The first line is return "How are you doing?". The function ends there with that string as its result.
  2. println("Hey bro!") is skipped. It sits after the return, so the function has already finished when execution would reach it.
  3. question receives the returned string. The call in mainFunction is an assignment, so the result is kept.
  4. 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:

  1. mainFunction() starts and meets val question = greetAndReturn().
  2. Control moves into greetAndReturn, which immediately executes its return with "How are you doing?".
  3. Control comes back to mainFunction with that string, and question is bound to it. No output yet.
  4. println(question) writes How 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 println inside greetAndReturn to 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 mainFunction passes it to println.

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 declared String. If the block ended with the println, its type would be Unit and the code would not compile (Found: Unit, Required: String). The final "Bye!" gives the block a String value, but it never becomes the result, because the return has already ended the function.

Common mistakes

  • Reading code as if every line always runs. Lines after return in the same block are unreachable.
  • Thinking return prints. The value only reaches the console because mainFunction prints 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

  • return ends the function immediately.
  • Code after a return in the same function body doesn't run.
  • To print something and return something, print first and return last.
Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free