Start learning free

LESSON 33 OF 415 · FUNCTIONS I

Execution Flow in Swift: Function Calls Quiz

Quiz Read the full explanation

Quiz: What would this code produce if we were to copy-paste it in a code editor and run it?

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

Important Note:

Do not use an actual code editor to get the answer! It would defy the whole purpose of the quiz!

Pick your answer

Next lesson

SWIFT · FUNCTIONS I

Quiz Indentation 1 in Swift, explained

Problem overview

The quiz shows two functions, and the second one calls the first:

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

The question is what the program prints. The options are:

  • A: Hey coder! and then Welcome back!.
  • B: only Welcome back!.
  • C: an error.
  • D: Welcome back! and then Hey coder!.

You answer with a constant named answer that holds the letter.

Approach: follow the flow of execution

The execution flow is the order in which statements actually run. It is not always the order in which they appear in the file. A function call sends the flow into that function's body. When the body finishes, the flow returns to the line right after the call.

So the way to solve the quiz is to start from the main program and follow it, jumping into a function each time it is called.

Step by step

  1. Skip welcomeUser's definition. At the top of the code it is only declared. Declaring doesn't run the body, so Hey coder! isn't printed yet.
  2. Start in mainFunction. It is the lesson's main program, and its statements run in the order they are written.
  3. The first statement is print("Welcome back!"). The output so far is Welcome back!.
  4. The second statement is the call welcomeUser(). The flow jumps into that function, runs print("Hey coder!"), and then returns.
  5. Nothing else is left, so the program ends with this output:
Welcome back!
Hey coder!

That is option D.

Why the other options are wrong

  • A follows the order of the definitions in the file. welcomeUser is written first, but being written first doesn't make it run first. Only a call makes it run.
  • B forgets the call. welcomeUser() is in mainFunction, so its body does run.
  • C expects an error, but the code is valid. A function can call another function, whether the called one is defined above or below it.

The solution in Swift

let answer = "D"

/* Explanation:

We first only define the welcomeUser() function, so the print("Hey coder!") won't actually be executed for now.

The mainFunction() is our "main program" so all the statements inside there will be executed in the order they are written.

First one is the "print("Welcome back!")", which will be executed and the output so far is:
Welcome back!

Then, the "welcomeUser()" function call will be executed.
So the code inside the function body (print("Hey coder!")) will get executed now and the output is:
Welcome back!
Hey coder!
*/

The text in quotes is a string, and the comment block is ignored by Swift.

Common mistakes

  • Reading the file from top to bottom. The execution flow starts in the main program. The order of the definitions in the file doesn't decide the order of the output.
  • Forgetting that a call runs the body now. The call to welcomeUser() prints its line at that moment, between the statements around it. If mainFunction had one more print after the call, its text would come after Hey coder!.
  • Dropping the call's output. A call isn't just a name. The body of the function runs completely before the next line of the caller.

Try a variation

Suppose mainFunction were written like this:

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

Now the call comes first, so the output is Hey coder! followed by Welcome back!, which is option A. Moving one line in the main program changes the whole result, and that is why tracing the flow matters.

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