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
- 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. - Compare the body with the expected output. The expected first line is
Hey coder!, but the first statement printsWelcome back!. - Swap the two
printlines. Put theHey coder!line first and theWelcome back!line second. - Leave the rest alone.
mainFunctionand 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
printends its line. That is why the two messages appear on separate lines and no\nis 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
printlines 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!.
