Functions and Closures
At this point, you can write reasonably complex programs that can make decisions and repeat instruction sequences. You can also store data for your programs using collection types. As the programs you write grow in size and complexity, it will become harder to comprehend what they do.
To make large programs easier to understand, Swift allows you to create functions, which let you combine a number of instructions together and execute them by calling a single name. You can also create closures, which lets you combine a number of instructions together without a name and assign it to a constant or variable.
By the end of this chapter, you’ll have learned about functions, nested functions, functions as return types, functions as arguments, and the guard
statement. You’ll also have learned how to create and use closures.
The following topics will be covered in this chapter:
- Understanding functions
- Understanding closures ...