Problem overview
The program should print two messages, each on its own line:
Hey coder!
Welcome back!
Instead it prints them in the opposite order:
Welcome back!
Hey coder!
The task is to fix the code so the output comes out in the expected order. This is a buggy-code lesson: the code compiles and runs, but it does the wrong thing.
The code and what it does
def welcomeUser(): Unit = {
println("Welcome back!")
println("Hey coder!")
}
def mainFunction(): Unit = {
welcomeUser()
}
Running the program means running mainFunction. Its body has one statement, the call welcomeUser(), and that call is made exactly once. So the order of the output is decided entirely inside welcomeUser.
What goes wrong
Scala runs the statements of a function body from the top to the bottom, in the order they are written. It never reorders them. In welcomeUser, the println("Welcome back!") line is written first, so it runs first. The line printing Hey coder! comes second, so it runs second. The output matches the code exactly, and the code is simply in the wrong order.
The cause isn't the call in mainFunction. Calling the function once, earlier or later, doesn't change what the function prints. Only the order of the two println lines does.
Fixing it step by step
- Find where the order is decided. The only call is
welcomeUser(), so the function body is the place to look. - Compare the lines with the expected output. The first expected line is
Hey coder!, but the firstprintlnprintsWelcome back!. - Swap the two
printlnlines. Put theHey coder!line first and theWelcome back!line second. - Leave everything else alone. The function names, the call and the
Unitresult types are all correct.
The fixed code in Scala
def welcomeUser(): Unit = {
println("Hey coder!")
println("Welcome back!")
}
def mainFunction(): Unit = {
welcomeUser()
}
/* Explanation:
Remember, the statements inside a function are executed in the exact order we write them.
That's why "Welcome back!" gets printed before "Hey coder!" in the console.
We have to swap the "println()" statements' order inside our function.
*/
Running mainFunction() now prints Hey coder! and then Welcome back!.
A few details about this code:
- Every
printlnends its line. That is why each message sits on its own line.printwould not add the line break. - Each call runs the body from the top. If
mainFunctioncalledwelcomeUser()twice, the pair of lines would be printed twice, in the same order each time. - The comment block does nothing. Text between
/*and*/is ignored by the compiler.
Calling a method before its definition
The lesson's title suggests that the order of definition and call might matter. For the top-level functions in this lesson, what matters is when the call runs, not where the definition sits in the file. In fact, inside a class or object, a method can be called from code that is written above its definition. This program prints called before its definition:
object Demo {
def later(): Unit = {
early()
}
def early(): Unit = println("called before its definition")
def main(args: Array[String]): Unit = later()
}
What does need care is the order of statements that run one after another. A statement can only use what has already been set up above it, and the output follows the order of the println calls.
Common mistakes
- Changing the call. Moving or repeating
welcomeUser()doesn't change the order of the two messages. - Editing the text. The messages are right. Retyping them differently would break the output.
- Swapping the function definitions. Putting
mainFunctionabovewelcomeUserhas no effect on the output order. - Typing the messages by hand into
mainFunction. That repeats code and moves away from the function-based design.
Testing
Run the fixed program and compare it to the expected output, line by line. It should print exactly two lines: Hey coder! followed by Welcome back!.
