Start learning free

LESSON 30 OF 415 · FUNCTIONS I

Defining a Function Without Calling It 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 sayMyName() {
	print("Heisenberg!")
}

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 Defining Without Calling in Swift, explained

Problem overview

The quiz shows a function that is defined and never called:

func sayMyName() {
    print("Heisenberg!")
}

The question is what happens when this code is run. The options are that it prints Heisenberg!, that it produces errors, that it prints nothing, or that it prints the word sayMyName. You answer by assigning the option's letter to a constant named answer.

Approach: ask what actually runs

When you read code to predict its output, go through the statements that execute, in order, and ignore the ones that only declare something. A func declaration is a declaration. It tells Swift that a function called sayMyName exists and what its body is. It does not run the body.

The body runs only when the function is called, which means writing its name followed by parentheses: sayMyName(). The code in the quiz has no such line, so the print statement is never reached.

Checking each option

  • A: prints Heisenberg!. This would need a call. There isn't one.
  • B: produces errors. The code is valid. The header has a name, parentheses and a body in braces, and print is a real function that accepts a string.
  • C: prints nothing. Correct. The function is defined and never used, so no output appears.
  • D: prints sayMyName. Function names are never printed on their own. The name only matters where the function is called.

The solution in Swift

The answer is C. You write it as a constant, with the text of the answer in double quotes:

let answer = "C"

/* Explanation:

Remember, the code inside a function will not be executed unless we call that function!
In our code, we only define the function sayMyName(), without calling it afterwards, so the print() won't be executed and nothing will be printed to the console.

*/

The block between /* and */ is a comment. Swift ignores it, so only the first line does anything. Quotes are required: let answer = C would look for a value named C and fail to compile, because the answer is a string and not a name.

Using let is the right choice here because the answer is chosen once and never changes.

Reading code the way Swift runs it

A useful habit for every quiz like this is to make a short list of what the program does, line by line:

  1. func sayMyName() { ... } registers a function. Nothing is printed.
  2. The code ends. No other statement exists.

The list contains no printing step, so the output is empty. If the code had a second function that called sayMyName(), you would also check whether that function is called by anything, because a call inside a function that never runs is not executed either.

What would change the answer

Add a call below the definition and the output changes:

func sayMyName() {
    print("Heisenberg!")
}

sayMyName() // Heisenberg!

With the call the program prints Heisenberg! once, and each extra call prints it again. Without it, nothing is printed.

Common mistakes

  • Treating a definition as an action. Seeing print in the code and picking option A. The print is inside a body that nobody calls.
  • Expecting an error. An unused function is legal, and the compiler doesn't complain about it.
  • Typing the answer without quotes or in lowercase. The value must be the string "C".
Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
Start learning free