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!andLet's code!, then the same two lines once more. - B:
Hello!twice, thenLet's code!twice. - C:
Hello!andLet'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.
- A definition does nothing by itself. The body of
greetCoderruns only when something calls it. - 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
- Skip the definitions. The first lines only define
greetCoder, so nothing is printed there. - Enter
mainFunction. It is the main program, and its statements run in the order they are written. - The first call.
greetCoder()runs both of itsprintstatements. The output so far isHello!followed byLet's code!. - The second call. The same body runs again from the top, so
Hello!andLet's code!are printed once more. - 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
| Step | What runs | Printed |
|---|---|---|
| 1 | first greetCoder(), first statement | Hello! |
| 2 | first greetCoder(), second statement | Let's code! |
| 3 | second greetCoder(), first statement | Hello! |
| 4 | second greetCoder(), second statement | Let'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 inmainFunction. 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.
