Start learning free

LESSON 33 OF 414 · FUNCTIONS I

Execution Flow in Scala: Tracing Two Functions

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 welcomeUser(): Unit = {
	println("Hey coder!")
}
def mainFunction(): Unit = {
	println("Welcome back!")
	welcomeUser()
}

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 Indentation 1 in Scala, explained

Problem overview

The quiz asks what this code prints when it runs:

def welcomeUser(): Unit = {
    println("Hey coder!")
}
def mainFunction(): Unit = {
    println("Welcome back!")
    welcomeUser()
}

The options are: Hey coder! followed by Welcome back! (A), only Welcome back! (B), errors (C), or Welcome back! followed by Hey coder! (D). You answer by assigning the letter of your choice to a string.

Approach: follow the flow of execution

Execution flow is the order in which statements run. Two rules cover this quiz.

  1. A def only defines a function. Its body doesn't run at that point.
  2. Statements in a body run from top to bottom. A call to another function pauses the current body, runs the other function's body in full, and then continues with the next line.

Trace mainFunction:

  1. The first statement is println("Welcome back!"). It prints Welcome back!.
  2. The second statement is the call welcomeUser(). Control jumps into welcomeUser, whose only statement prints Hey coder!.
  3. That body ends, and control returns to mainFunction, which has nothing left to do.

The output is:

Welcome back!
Hey coder!

That is option D.

Why the other options are wrong

  • A reverses the order. It would be right if welcomeUser() were called before the println("Welcome back!") line. The order of lines in mainFunction is what counts, not the order in which the two functions are defined.
  • B forgets the call. A call does run the function body, so Hey coder! is printed.
  • C expects errors, but the code is valid. welcomeUser is defined before it is used, and both definitions have = and a body in braces.

What if the call came first?

Move the call above the print and the answer changes. With this version of mainFunction:

def mainFunction(): Unit = {
    welcomeUser()
    println("Welcome back!")
}

the output is Hey coder! followed by Welcome back!, which is option A. Only the order of the statements inside mainFunction changed, and the output followed it.

Indentation and braces

The code is indented, and the indentation helps a reader see which lines belong to which function. With braces, though, the compiler doesn't depend on it. The braces mark where each body starts and ends. In Scala 3, the braces can be left out and indentation takes over, but a block that uses braces is the form the lessons use. Mixing tabs and spaces in the same block is rejected, so keep the indentation consistent.

The solution in Scala

val answer: String = "D"
/* Explanation:
We first only define the welcomeUser() function, so the println("Hey coder!") won't actually be executed for now.
The mainFunction() is our "main program" so all the statements inside there will be executed in the order they are written.
First one is the "println("Welcome back!")", which will be executed and the output so far is:
Welcome back!
Then, the "welcomeUser()" function call will be executed.
So the code inside the function body (println("Hey coder!")) will get executed now and the output is:
Welcome back!
Hey coder!
*/

A few Scala details:

  • val answer: String = "D" declares a value that can't be reassigned. The type is optional, because Scala infers String from the literal.
  • The quotes are required. Without them, D is read as a name, and the compiler reports Not found: D.
  • A plain assignment isn't enough. Writing just answer = "D" assigns to a variable that doesn't exist yet, so the variable must be declared with val or var.

Common mistakes

  • Ordering the output by definition order. The function that is defined first doesn't print first. The order of the statements being executed decides.
  • Skipping the nested call. When a function body calls another function, that function's output appears at the point of the call.
  • Treating the definitions as output. Defining a function prints nothing.
Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free