Closures are also referred to as anonymous functions, and this is the best way to explain them. Closures are functions without a name and, like other functions, they can take a set of input parameters and can return an output. Closures behave like other primary types. They can be assigned, stored, passed around, and used as input and output to functions and other closures.
In this recipe, we will explore how and when to use closures in our code.
Getting ready
We will continue to build on our contacts app example from earlier in this chapter, so you should use the same playground as in the previous recipes.
If, however, you are implementing this in a new playground, first add the relevant code from the previous recipes:
struct PersonName {
let givenName: String
let middleName: String
var familyName: String
func fullName() -> String {
return "\(givenName) \(middleName) \(familyName)"
}
mutating...