Start learning free

LESSON 31 OF 414 · FUNCTIONS I

Statement Order in a Scala Function: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to define and call a function that would print the messages Hey coder! then Welcome back! to the console.

So when we ran the code, we expected it to print:

Hey coder!
Welcome back!

but it seems like we made some mistakes because when we run our code we get:

Welcome back!
Hey coder!

Assignment:

Your task is to fix our code such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · FUNCTIONS I

    Buggy Code Call Before Definition in Scala, explained

    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

    1. Find where the order is decided. The only call is welcomeUser(), so the function body is the place to look.
    2. Compare the lines with the expected output. The first expected line is Hey coder!, but the first println prints Welcome back!.
    3. Swap the two println lines. Put the Hey coder! line first and the Welcome back! line second.
    4. Leave everything else alone. The function names, the call and the Unit result 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 println ends its line. That is why each message sits on its own line. print would not add the line break.
    • Each call runs the body from the top. If mainFunction called welcomeUser() 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 mainFunction above welcomeUser has 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!.

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free