Start learning free

LESSON 31 OF 415 · FUNCTIONS I

Statement Order in a Swift 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

    SWIFT · FUNCTIONS I

    Buggy Code Call Before Definition in Swift, explained

    What the code is supposed to do

    The program should print two messages, each on its own line, in this order:

    Hey coder!
    Welcome back!
    

    It prints them the wrong way round:

    Welcome back!
    Hey coder!
    

    Here is the code you start with:

    func welcomeUser() {
        print("Welcome back!")
        print("Hey coder!")
    }
    
    func mainFunction() {
        welcomeUser()
    }
    

    Nothing here fails to compile and nothing crashes. The program does exactly what its code says, and the code says the wrong thing. Fixing it means finding which part controls the order.

    What goes wrong

    Swift runs the statements in a function body from top to bottom, one after another. mainFunction makes a single call to welcomeUser(), so the call happens once and in the right place. The order of the output is decided inside welcomeUser, by the order of its two print lines. The first line in the body is print("Welcome back!"), so that message comes out first.

    The calls aren't the problem, because there is only one. The problem is the sequence of statements inside the function.

    Fixing it step by step

    1. Find where the output order is decided. The only function that prints is welcomeUser, and it is called once, so its body is the place to look.
    2. Compare the body with the expected output. The expected first line is Hey coder!, but the first statement prints Welcome back!.
    3. Swap the two print lines. Put the Hey coder! line first and the Welcome back! line second.
    4. Leave the rest alone. mainFunction and the call are correct, and the text of each message already matches the expected output.

    The fixed code in Swift

    func welcomeUser() {
        print("Hey coder!")
        print("Welcome back!")
    }
    
    func 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 "print()" statements' order inside our function.
    
    */
    

    The comment block at the end is only an explanation and has no effect on the program.

    Details worth knowing

    • Order of statements matters. Moving a line up or down inside a body changes what the program does, even when every line is individually correct.
    • Each print ends its line. That is why the two messages appear on separate lines and no \n is needed.
    • Order of definitions is a different thing. In Swift, global functions can be declared above or below the code that calls them. What controls the printed order is the order in which the statements are executed, and that comes from the bodies and the calls.

    To see the idea with a different example, think of this function:

    func countDown() {
        print("2")
        print("1")
        print("Go!")
    }
    

    Every call prints 2, 1 and Go! in that order. To change the order of the output, you change the order of the lines.

    Common mistakes

    • Editing the call instead of the body. Adding a second welcomeUser() call, or moving the call, doesn't reorder the lines inside the function.
    • Changing the message text. Swapping the strings while leaving the print lines in place would work, but swapping the lines is the clearer fix.
    • Trying to fix it with indentation. Indentation doesn't change the order of execution in Swift.
    • Typing the messages differently. The comparison is exact, including the capital letters and the exclamation mark in Hey coder!.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free