Problem overview
The starting program has one function, mainFunction, and it prints the same three messages three times:
func mainFunction() {
print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")
print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")
print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")
}
The task is to produce the same output in a better way: create a function named printMessages that prints the three messages, and call it three times from mainFunction.
Approach: move the repeated block into a function
The nine print lines are three copies of one block. A block that repeats is a sign it should be a function. The block moves into the function's body once, and each copy in the main program becomes a call.
The program's behaviour stays the same. Its output is still nine lines. What changes is the structure: the messages exist in one place, and changing one of them means editing a single line.
Step by step
- Define
printMessagesabovemainFunction. It has no parameters, so the parentheses are empty, and it has no return type. - Put the three
printcalls in its body, in the same order as before:Hello AlgoCademy!,Coding is amazing!, thenFunctions are so useful!. - Empty
mainFunction's body of those nineprintlines. - Call
printMessages()three times insidemainFunction, one call for each repetition. The parentheses are required even though there are no arguments.
The solution in Swift
// Define and implement the function printMessages here:
func printMessages() {
print("Hello AlgoCademy!")
print("Coding is amazing!")
print("Functions are so useful!")
}
func mainFunction() {
// Call printMessages 3 times here:
printMessages()
printMessages()
printMessages()
}
The lines starting with // are comments. Swift ignores them.
Running mainFunction prints this, which is the same output as the original nine lines:
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Details worth knowing
- A function with no
->returns nothing.printMessagesonly has an effect, printing. There is no value to use afterwards. - Defining isn't calling. If you write the function and leave
mainFunctionempty, nothing is printed. - Parentheses make the call. A bare
printMessagesonly names the function. Swift reports a line like that as an error. - Calls run in order. The three calls run one after another, and each finishes before the next starts, so the output is three complete groups, not interleaved lines.
Edge cases and common mistakes
- Calling the function the wrong number of times. Two calls print six lines. Four print twelve. The task needs three.
- Leaving the original
printlines inmainFunction. They print in addition to the calls, so the output has too many lines. - Changing the text. A different capital letter, a missing exclamation mark or a different order of the three messages changes the output.
- A different name. The function must be named
printMessagesexactly, because the task refers to it by name. - Adding parameters. This version has none. The function does the same work on every call.
Testing
To check your work, count the lines the program prints. There should be nine, in repeating groups of the three messages. Then change one message inside printMessages: all three groups should change together, which shows the function is doing its job.
