Start learning free

LESSON 30 OF 291 · FUNCTIONS I

Defining Without Calling in Scala: Why Nothing Prints

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 sayMyName(): Unit = {
	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

SCALA · FUNCTIONS I

Quiz Defining Without Calling in Scala, explained

Problem overview

The quiz shows a single Scala function and asks what happens when the code runs:

def sayMyName(): Unit = {
    println("Heisenberg!")
}

There are four options: it prints Heisenberg! (A), it produces errors (B), it prints nothing (C), or it prints sayMyName (D). You answer by assigning the letter to a variable called answer, so the task is to reason about the code, not to run it.

Approach: separate defining from calling

Two different things can happen to a function, and the quiz tests whether you can tell them apart:

  • Defining it with def tells Scala the function exists and what its body is. The body is stored, not executed.
  • Calling it, by writing its name followed by parentheses, runs the body.

The code above contains a definition and nothing else. There is no sayMyName() anywhere, so the body never executes and the println inside it never runs.

Checking each option

  1. A: it prints Heisenberg!. This would be true if the code contained the call sayMyName(). It doesn't, so nothing prints.
  2. B: it produces errors. The definition is valid. It has def, a name, empty parentheses, the result type Unit, an = and a body in braces, so it compiles. Defining a function and never using it is not an error.
  3. C: it prints nothing. This is the right answer.
  4. D: it prints sayMyName. A function's name is never printed by defining it. The only text the code can print is the string inside println, and that happens only on a call.

The answer is C.

The solution in Scala

The starter code gives you a Solution class with a method that prints the value of answer. Your job is to declare that variable with your choice at the top of the method:

class Solution {
    def magicNumber(n: Int): Unit = {
        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 & nothing will be printed to the console.
        */
        println(answer)
    }
}

Some Scala details in these lines:

  • The type annotation. var answer: String = "C" states the type, String. Scala would infer it from the literal, so var answer = "C" means the same.
  • Quotes matter. The letter is a string in double quotes. Without the quotes, C would be read as a name, and the compiler reports Not found: C.
  • Declare it before use. The println(answer) line refers to answer, so the declaration has to come first. Without it, the compiler says Not found: answer.
  • var and val. The variable is never reassigned, so val would also work and is the more usual choice for a value that doesn't change. var is fine here.
  • Indentation. Scala 3 rejects a block that mixes tabs and spaces in the indentation, so indent consistently.

Seeing the difference

If the quiz code had one more line, the answer would change. With a call added after the definition:

def sayMyName(): Unit = {
    println("Heisenberg!")
}

sayMyName()

this prints Heisenberg! once, which is option A. Each additional call prints it again.

Common mistakes

  • Treating a definition as a call. Seeing a println in the code and assuming it runs. A println inside a function body runs only when the function is called.
  • Expecting an error for an unused function. Unused functions are legal.
  • Leaving out the quotes around the letter.
  • Running the code to find the answer. The quiz is about predicting the result from the rules, which is the skill the later lessons build on.
Keep going with Scala1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free