Start learning free

LESSON 35 OF 415 · FUNCTIONS I

Creating a Function in Swift: Exercise printMessages

Practice Read the full explanation

We've written this code:

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!")
}

Write the same code in a better way by creating and calling a function printMessages()

STEP 1 OF 5
    Ctrl + Enter to check

    SWIFT · FUNCTIONS I

    Exercise Create Function in Swift, explained

    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

    1. Define printMessages above mainFunction. It has no parameters, so the parentheses are empty, and it has no return type.
    2. Put the three print calls in its body, in the same order as before: Hello AlgoCademy!, Coding is amazing!, then Functions are so useful!.
    3. Empty mainFunction's body of those nine print lines.
    4. Call printMessages() three times inside mainFunction, 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. printMessages only has an effect, printing. There is no value to use afterwards.
    • Defining isn't calling. If you write the function and leave mainFunction empty, nothing is printed.
    • Parentheses make the call. A bare printMessages only 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 print lines in mainFunction. 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 printMessages exactly, 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.

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