Start learning free

LESSON 32 OF 415 · FUNCTIONS I

Calling a Function Twice in Swift: 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 greetCoder() {
	print("Hello!")
	print("Let's code!")
}

func mainFunction() {
	greetCoder()
	greetCoder()
}

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 Calling A Function in Swift, explained

Problem overview

The quiz shows two functions:

func greetCoder() {
    print("Hello!")
    print("Let's code!")
}

func mainFunction() {
    greetCoder()
    greetCoder()
}

The question is what the program prints. The four options differ in how the two messages are repeated:

  • A: Hello! and Let's code!, then the same two lines once more.
  • B: Hello! twice, then Let's code! twice.
  • C: Hello! and Let's code! once.
  • D: the two lines three times.

You answer by assigning the letter to a constant named answer.

Approach: trace the program line by line

Two rules are enough to solve this.

  1. A definition does nothing by itself. The body of greetCoder runs only when something calls it.
  2. Every call runs the whole body from its first statement to its last, and then the program goes back to the line after the call.

Step by step

  1. Skip the definitions. The first lines only define greetCoder, so nothing is printed there.
  2. Enter mainFunction. It is the main program, and its statements run in the order they are written.
  3. The first call. greetCoder() runs both of its print statements. The output so far is Hello! followed by Let's code!.
  4. The second call. The same body runs again from the top, so Hello! and Let's code! are printed once more.
  5. Put the output together:
Hello!
Let's code!
Hello!
Let's code!

That is option A. Because both messages sit in the function, the function is the only place that decides what each call prints, and the number of calls decides how many times the pair appears.

The same trace as a table

StepWhat runsPrinted
1first greetCoder(), first statementHello!
2first greetCoder(), second statementLet's code!
3second greetCoder(), first statementHello!
4second greetCoder(), second statementLet's code!

A function body is a unit that runs completely each time. If mainFunction had three calls, the table would simply get four more rows, and the output would be the two lines three times. That is what option D describes, but this program has only two calls.

Why the other options are wrong

  • B would mean the two calls run their first statements before either runs its second. A call finishes completely before the next statement of the caller starts.
  • C counts one call. The function is called twice.
  • D counts three calls. There are exactly two greetCoder() lines in mainFunction. The definition at the top isn't a call.

The solution in Swift

let answer = "A"

/* Explanation:
In the first 4 lines of code we only define the function, so no "print()" statement will actually get executed.

Then, inside the main function, we call the function for the first time, so all the statements inside the function body will get executed from top to bottom.
So, the first function call will print "Hello!", then it will print "Let's code!" on two separate lines, and then this function call ends.
The output so far is:
Hello!
Let's code!

Then we call the function for the second time, and again all the statements inside the function body will get executed from top to bottom.
So the second function call will again print "Hello!", then it will print "Let's code!" on two separate lines, and then the function call ends.
So the output is:
Hello!
Let's code!
Hello!
Let's code!
*/

The answer is a string, so the letter goes in double quotes. The comment block is ignored by Swift.

Testing your understanding

Suppose greetCoder had its two print lines swapped and were called once. The output would be Let's code! followed by Hello!, because the lines of a body run in the order they are written. The number of calls and the order of the statements are the two things to check in any quiz of this kind.

Common mistakes

  • Counting the definition as a call. That gives three runs (option D). Only a name followed by parentheses calls the function.
  • Printing once. Thinking a function runs a single time, however often it is called (option C). Each call runs the body again.
  • Interleaving the lines. Mixing the statements of two calls (option B). Calls don't overlap: one finishes before the next begins.
Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free