Start learning free

LESSON 32 OF 414 · FUNCTIONS I

Calling a Function Twice in Scala: Output 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?

def greetCoder(): Unit = {
	println("Hello!")
	println("Let's code!")
}

def mainFunction(): Unit = {
	greetCoder()
	greetCoder()
}

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 Calling A Function in Scala, explained

Problem overview

The quiz shows two functions and asks what the code prints when it runs:

def greetCoder(): Unit = {
    println("Hello!")
    println("Let's code!")
}
def mainFunction(): Unit = {
    greetCoder()
    greetCoder()
}

The options differ in the order and the number of lines:

  • A: Hello!, Let's code!, Hello!, Let's code!
  • B: Hello!, Hello!, Let's code!, Let's code!
  • C: Hello!, Let's code!
  • D: the pair of lines three times

You answer by declaring a string with the letter of the right option.

Approach: trace the execution

Work through the program the way Scala does, one statement at a time.

  1. The two def blocks only define functions. Nothing prints while they are being defined.
  2. Running mainFunction executes its body from top to bottom. The first statement is the call greetCoder().
  3. That call runs the whole body of greetCoder: it prints Hello!, then Let's code!, and the call ends. The output so far is two lines.
  4. Control returns to mainFunction, which reaches its second statement, another greetCoder(). The whole body runs again, printing the same two lines.

The complete output is:

Hello!
Let's code!
Hello!
Let's code!

That is option A.

A quick way to count

The number of printed lines is the number of calls times the number of println statements in the body: 2 calls times 2 lines gives 4 lines. A third call would add two more lines, which is what option D describes.

Anything written between the calls shows up between the copies of the output. If mainFunction had a println("---") between the two calls, the output would be:

Hello!
Let's code!
---
Hello!
Let's code!

The body of greetCoder stays intact in both copies, because a call finishes its whole body before the next statement of mainFunction runs.

Why the other options are wrong

  • B would mean the function's two lines are interleaved across the calls. A call finishes its whole body before the next statement runs, so the two Hello! lines can't come back to back.
  • C counts one call. The code makes two, and each runs the body in full.
  • D counts three calls. Count the lines of the form greetCoder() inside mainFunction: there are two.

The solution in Scala

val answer: String = "A"
/* Explanation:
In the first 4 lines of code we only define the function, so no "println()" statement will actually get executed.
Then, inside the main function, we call the function for the first time, so all the statements inside the function body will get executed from top to bottom.
So, the first function call will print "Hello!", then it will print "Let's code!" on two separate lines, and then this function call ends.
The output so far is:
Hello!
Let's code!
Then we call the function for the second time, and again all the statements inside the function body will get executed from top to bottom.
So the second function call will again print "Hello!", then it will print "Let's code!" on two separate lines, and then the function call ends.
So the output is:
Hello!
Let's code!
Hello!
Let's code!
*/

The Scala details in this line:

  • val declares a value that can't be reassigned. It is the natural choice for an answer you set once. var would also work.
  • The type annotation is optional. val answer: String = "A" and val answer = "A" mean the same, because Scala infers String from the literal.
  • The letter is a string. It needs double quotes. A bare A is treated as a name, and the compiler reports Not found: A.
  • The comment block is ignored. Everything between /* and */ is skipped by the compiler.

Common mistakes

  • Reading the function as printing once. The body runs once per call, so two calls give two copies of the output.
  • Merging the calls. Each call prints its two lines together, in the body's order.
  • Counting the definition as a call. The line def greetCoder(): Unit = { doesn't run anything. Only greetCoder() with parentheses does.
  • Running the code to find the answer. Tracing the code by hand is the skill being practised here.
Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free