Start learning free

LESSON 42 OF 348 · FUNCTIONS I

Forgotten Return in Kotlin: Quiz on a Missing 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?

fun getDouble(n: Int): Int {
	n * 2
}

fun mainFunction() {
	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

KOTLIN · FUNCTIONS I

Quiz Forgotten Return in Kotlin, explained

Problem overview

The quiz shows a function and a caller, and asks what happens when the code is compiled and run. There are three options: it prints 20, it produces errors, or it prints 10. You answer by assigning the letter of your choice to answer.

fun getDouble(n: Int): Int {
    n * 2
}
fun mainFunction() {
    val result = getDouble(10)
    println(result)
}

Reading the code like the compiler

Start with the header of getDouble: fun getDouble(n: Int): Int. The : Int after the parentheses is the return type. It is a promise that every call gives back an Int.

Now look at the body. The only line is n * 2. It is a valid expression, and it computes 20 when n is 10. But nothing uses the result. There is no return in front of it, and it isn't stored or printed. In a function with a block body, written with curly braces, the value of the last expression is not returned automatically.

The compiler therefore sees a function that promises an Int and can reach its closing brace without returning anything. It rejects the code with the error missing return statement. Compilation fails, so the program never runs and nothing is printed. That rules out options A and C, which both describe printed output.

Why the other options are tempting

  • Option A, printing 20. This is what the author intended, and what you would see after adding the word return. The code as written never gets as far as computing it.
  • Option C, printing 10. This would be right if the function handed back its argument unchanged. Nothing in the code does that.

The right answer is B: it would produce errors.

Step by step

  1. Find the declared return type. It is Int, so the function must give back an integer.
  2. Look for a return in the body. There is none.
  3. Conclude that compilation fails. The compiler checks this before running anything.
  4. Write the answer. Declare a val named answer of type String and assign it the letter.

The solution in Kotlin

val answer: String = "B"

The line declares a read-only variable. val means it can't be reassigned, : String is its type, and the letter goes in double quotes because it is text. The letter must be a capital B, since the comparison is exact.

How the code is fixed

There are two ways to make getDouble compile and print 20. The first adds the keyword the function was missing:

fun getDouble(n: Int): Int {
    return n * 2
}

The second uses an expression body, which returns its single expression without the word return:

fun getDouble(n: Int): Int = n * 2

Dropping the return type is not a fix. A function with a block body and no return type returns Unit, so val result = getDouble(10) would receive Unit, not 20, and the program would print kotlin.Unit.

Common mistakes

  • Assuming the last line is returned. That is true for expression bodies and for lambdas, but not for a function with braces.
  • Thinking a computed value is kept. An expression whose result isn't used or returned is simply dropped.
  • Expecting a run-time error. A missing return in a non-Unit function is caught at compile time, so there is no partial output.
  • Writing the answer without quotes, as in val answer: String = B. Without quotes, B is a name, not text.
Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free