In the Chapter 4, Range Operators and Loops, you learned about how to repeat instruction sequences using loops. Note that each sequence has a number of instructions, and if there are a lot of instructions, then it becomes unclear what the sequence is supposed to do. Swift allows you to combine a number of instructions together and execute them by calling a single name. This is called a function. It is also possible to combine a number of instructions together without a name and assign it to a constant or variable. This is called a closure.
By the end of this chapter, you'll have learned how to create and use functions, nested functions, functions as return types, and functions as arguments. You'll also have learned how to create and use closures.
The following topics will be covered:
- Understanding functions
- Understanding closures