Start learning free

LESSON 31 OF 348 · FUNCTIONS I

Statements Run in Order in Kotlin: 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

    KOTLIN · FUNCTIONS I

    Buggy Code Call Before Definition in Kotlin, explained

    What the code is supposed to do

    The program should print two lines, Hey coder! first and Welcome back! second. It defines a function, welcomeUser, that holds the two println calls, and a second function, mainFunction, that calls it. Instead of the expected order, the output is reversed:

    Welcome back!
    Hey coder!
    

    This is the code you start with:

    fun welcomeUser() {
        println("Welcome back!")
        println("Hey coder!")
    }
    
    fun mainFunction() {
        welcomeUser()
    }
    

    Your job is to change it so the two messages come out in the right order.

    What goes wrong

    The code compiles and runs, so no error points at the mistake. The only problem is the order of the output. To find the cause, work out which code produces the output:

    1. mainFunction calls welcomeUser() once and does nothing else.
    2. The call runs the body of welcomeUser, and the body is a list of two statements.
    3. Kotlin executes the statements in a function body one at a time, from the first line down to the last. It never reorders them.

    The first statement in the body prints Welcome back!, and the second one prints Hey coder!. So the output has exactly the order of the lines in the file, and the bug is in the file. There is nothing wrong with the call, since the function is called once and in the right place.

    The fix, step by step

    1. Find where the order is decided. The only function with more than one statement is welcomeUser, so the order is decided there.
    2. Compare with the target. The expected output starts with Hey coder!, so that message has to be printed first.
    3. Swap the two println lines. Put println("Hey coder!") above println("Welcome back!").
    4. Leave mainFunction alone. It already calls the function exactly once, and the function should still run only once.

    The fixed code in Kotlin

    fun welcomeUser() {
        println("Hey coder!")
        println("Welcome back!")
    }
    
    fun mainFunction() {
        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.
    
    */
    

    A few Kotlin details that matter here:

    • Each println ends its line. That is why the two messages appear on separate lines, without any extra work.
    • The order of definitions is irrelevant. mainFunction could be written above welcomeUser and still work, because Kotlin knows all of a file's functions before anything runs. What counts is the order of statements inside a body, and the order of calls.
    • A block comment is ignored. The text between /* and */ at the end of the solution is a note for the reader and has no effect on what runs.

    To see the result yourself in a standalone program, add a main that calls mainFunction:

    fun main() {
        mainFunction()
    }
    

    With the fixed welcomeUser in the same file, that program prints:

    Hey coder!
    Welcome back!
    

    Common mistakes

    • Changing the call instead of the body. Adding another call to welcomeUser() doesn't reorder anything. It prints both lines a second time, in the same wrong order.
    • Rewriting the messages. The text of each message, including capitals and the exclamation mark, has to stay exactly as it is. Only the order changes.
    • Editing only one line. Retyping the first println to say Hey coder! and leaving the second one unchanged would print Hey coder! twice. Both lines move, or exchange their text.

    Why this matters

    Everything a program does is the result of statements executing in a known order: top to bottom within a body, with calls jumping into other functions and returning to the next line. When the output is wrong, trace which statement runs when, and ask which of them printed each line. Here, tracing a single call is enough to find the mistake.

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