Start learning free

LESSON 42 OF 414 · FUNCTIONS I

Quiz: Forgotten Return in a Scala Function

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 getDouble(n: Int): Int = {
	println(n * 2)
}

def mainFunction(): Unit = {
	val result = getDouble(10)
	println(result)
}

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 Forgotten Return in Scala, explained

The question

The quiz shows this code and asks what happens if you compile and run it:

def getDouble(n: Int): Int = {
    println(n * 2)
}

def mainFunction(): Unit = {
    val result = getDouble(10)
    println(result)
}

The options are: A, it prints 20; B, it produces errors; C, it prints 10. You answer by assigning the letter to a String named answer.

Working out the answer

Reason like the compiler, from the top.

  1. Read the promise. The header of getDouble ends in : Int, so the method claims to give back an Int.
  2. Find what the body produces. In Scala the value of a block is the value of its last expression. The last expression here is println(n * 2).
  3. Find the type of that expression. println prints text and returns Unit, the type that means "no useful value".
  4. Compare. The method promised an Int and delivers a Unit. The types don't match, so the compiler refuses the program with a type mismatch error.

The message looks like this:

-- [E007] Type Mismatch Error ---
3 |    println(n * 2)
  |    ^^^^^^^^^^^^^^
  |    Found:    Unit
  |    Required: Int

Because the program doesn't compile, mainFunction never runs, and nothing is printed. That rules out option A, which would need the program to print 20 at least once, and option C, which isn't a value the code could produce anywhere. The answer is B.

The answer in Scala

val answer: String = "B"

/* Explanation:
Notice that inside the function we only compute n * 2 but do nothing with the result afterwards, we do not return it from the function.

A function with no return statement inside it is called a "void" function and should be defined accordingly, like this:
def getDouble(n: Int): Unit = {
    println(n * 2)
}

But because we defined our function as an "Int" function ( def getDouble(n: Int): Int ), Scala expects an Int to be returned from the function.
And so when Scala notices that there is no actual "return" statement inside the function, it throws an error.
*/

The details that matter:

  • Printing is not returning. println shows a value on the screen. It doesn't hand anything back to the caller, and the value it does produce is Unit.
  • The missing return is not the real problem. Scala doesn't require the keyword: writing n * 2 as the last line would be a valid fix with no return at all. The error exists because the last expression has the wrong type.
  • Two ways to fix the function. Make the last expression an Int, as in def getDouble(n: Int): Int = { n * 2 }, or change the promise to : Unit if printing is all you want, and then drop the val result line, since there is no number to store.

What the program would do if it compiled

It helps to see why option A is tempting. If the header said : Unit instead of : Int, the program would compile. getDouble(10) would print 20, and result would hold (), the single value of type Unit, so the second println would print (). If the body ended with n * 2 and the header stayed : Int, the call would give back 20 and the program would print 20 once, from mainFunction. Neither is what the code as written does: it has to pass the type check first, and it fails.

Common mistakes

  • Choosing A. That would be true in a language where a missing return just gives a default value. In Scala a mismatched result type is a compile error.
  • Choosing C. The argument 10 is never printed by this code, with or without errors.
  • Answering with a lowercase letter or extra text. The answer is the single string "B".
  • Running the code to find out. The point of the quiz is to predict the compiler's behaviour by reading, which is a skill you need whenever a program is too large to run in your head.
Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free