What are Closures


Nothing freaked me out more than closures when I started my iOS career. Their syntax looked weird and utilizing them felt odd. Asking senior engineers about closures didn’t help neither. They usually gave me a verbose explanation full of abstractions. This article will explain what closures are clearly and succinctly.


Closures are anonymous functions. This means they have no name and don’t require a func keyword in their syntax. This next point is important. Because they have no name, you cannot write a closure by itself like you do for a function. You must either store the closure in a variable or set it as a type.


closure_syntax

The first line of code demonstrate a variable that stores a closure. The second line of code shows a variable that has a closure as its type. Likewise, you can create a function that has a parameter who takes in a closure as its value.


closure_syntax

The code above contains a function with two parameters. The first parameter takes in an integer, while the second parameters takes in a closure.


The syntax of a closure can be described as follows: (parameters) -> (return type). The empty parentheses you have seen in the previous example above simply means the closure takes in no parameters and returns nothing.


closure_syntax

The code above is the same function but with changes in the signature of the closure. A signature defines the input and output of a closure. Previously, the closure had a signature of no parameters, and returning nothing. We have changed the signature so that it takes in an integer and returns a boolean


The start of a closure’s body is introduced by the in keyword. This keyword indicates that the definition of the closure’s parameters and return type has finished, and the body of the closure is about to begin.


closure_syntax

If you remember, closures are anonymous functions so they must be stored into variables. In the example above, we create a variable called divisibleByFive which stores a closure. The signature of the closure defines that it takes in an integer and returns a boolean.


Once the parameters and return type is finished, the in marks the start of the closure body, this is where you write the code you want implemented. In our case, the body returns true if the number passed into it is divisible by 5.


Lastly, when a closure matches the signature of a function parameter, it can be passed into the parameter of that function.


closure_syntax

The divisibleByFive variable takes in a number and returns a boolean. Likewise the logic parameter of that function also takes in an integer and returns a boolean. This means that their signatures match. This allows us to pass divisiblebyFive as the logic parameter for the compareNumbers function. Since the closure is a function itself (an anonymous one), this case is an example of a nested function, which means a function inside a function.


There you have it, thats closures in a nutshell. They are anonymous functions you can store into variables or pass into functions. There are more advance details you’ll need to know of course, such as the fact that closures are reference types. But for now, you have a good basis. Becoming a software engineer is a journey so take it one step at a time.


code

Keep Coding!