Closures are blocks of code that you can pass to functions as parameters or store as variables or constants to be used later. Closures capture the state of the other variables around them. These constructs are often used when you want to perform operations that don’t need to happen immediately but depend on local state. Closures are also used frequently with functions to give callers a way of adding behavior to a function that would normally be out of scope.
One example of where closures are used is as an argument to the Swift standard library sorted function. This function takes an array and a closure as parameters. The array contains the items that need to be sorted, and the closure includes the instructions that will ...