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.
- A
defonly defines a function. Its body doesn't run at that point. - 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:
- The first statement is
println("Welcome back!"). It printsWelcome back!. - The second statement is the call
welcomeUser(). Control jumps intowelcomeUser, whose only statement printsHey coder!. - 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 theprintln("Welcome back!")line. The order of lines inmainFunctionis 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.
welcomeUseris 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 infersStringfrom the literal.- The quotes are required. Without them,
Dis read as a name, and the compiler reportsNot 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 withvalorvar.
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.
