Start learning free

LESSON 30 OF 348 · FUNCTIONS I

Defining a Function Without Calling It in Kotlin: Quiz

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 sayMyName() {
	println("Heisenberg!")
}

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 Defining Without Calling in Kotlin, explained

Problem overview

This is a multiple-choice quiz about a short piece of Kotlin code. The code defines a function named sayMyName whose body prints Heisenberg!, and it does nothing else. The question is what happens when this code is run, and the options are:

  • A: it prints Heisenberg!;
  • B: it produces errors;
  • C: it prints nothing;
  • D: it prints sayMyName.

You answer by giving the variable answer the letter of your choice. The point is to answer by reading the code, not by running it.

Approach: definition versus call

Ask two separate questions about any function you see.

  1. Is there a definition? A definition, which starts with fun, tells Kotlin that a function exists and what its body contains.
  2. Is there a call? A call is the function's name followed by parentheses, like sayMyName(). Only a call makes the body execute.

In the quiz there is a definition and no call. The statements inside the body, the println here, are stored as part of the function and not executed. Nothing in the code asks for them to run.

Step by step

  1. Look for a call. The only lines are the header fun sayMyName() {, the println and the closing brace. No other line mentions sayMyName.
  2. Rule out option A. The println is inside the body, which never runs, so Heisenberg! is never printed.
  3. Rule out option D. A function's name is never printed just because it is defined. The only way to see it would be to pass it to println as text, which this code doesn't do.
  4. Rule out option B. The definition is well-formed: the keyword, a name, parentheses, a body in braces and a call to the built-in println with a correct string. It compiles.
  5. Choose C. The code compiles, nothing calls the function and nothing is printed.
  6. Store the letter in answer.

The solution in Kotlin

class Solution {
    fun magicNumber(n: Int) {
        var answer: String = "C"

        /* Explanation:

        Remember, the code inside a function will not be executed unless we call that function!
        In our code, we only define the function sayMyName(), without calling it afterwards, so the println() won't be executed and nothing will be printed to the console.

        */

        println(answer)
    }
}

The code in the editor is a small template and has nothing to do with the quiz code itself. The line that matters is var answer: String = "C". A few Kotlin details about it:

  • The type comes after the name. answer: String declares a variable named answer whose type is String. The letter goes in double quotes, because it is text and not a name: "C" is a string, while a bare C would be read as a reference to something called C.
  • var versus val. Here var is used, which allows later reassignment. Since the answer is never changed, val would be the more natural choice. Both pass the lesson's tests.
  • The explanation is a comment. Everything between /* and */ is ignored by the compiler, so it can't affect the result.
  • The parameter n is unused. The function takes an Int that its body never reads. This is allowed, and it doesn't change the answer.

What running a call would do

If the quiz code did call the function, the result would change. This program defines sayMyName and calls it once from main, so it prints Heisenberg! a single time:

fun sayMyName() {
    println("Heisenberg!")
}

fun main() {
    sayMyName()
}
Heisenberg!

Without the call inside main, the same program compiles and prints nothing.

Edge cases and common mistakes

  • Reading the body as if it ran. The most common wrong answer is A. The println is right there, so it is tempting to see its output. Check for a call first.
  • Treating "nothing printed" as an error. A program with no output is valid. Errors come from the compiler rejecting the code, not from the lack of output.
  • Writing the answer without quotes or with the wrong case. The value must be the string "C", written in capital letters.
  • Running the quiz code to check. Trace the code by hand instead. That habit carries over to every later question about what code does.
Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free