Functions as Return Types

Now that you have more experience with functions and closures, recall from Chapter 12 that every function has a function type. A function type defines a function’s or closure’s parameter and return types. For example, a function that takes a String argument and returns an Int has the function type of (String) -> Int. Function types are frequently used to determine what sort of closure you need to satisfy a given parameter’s type or what sort of function needs to be returned.

In Swift, functions are first-class objects. One implication of this is that functions can return other functions as their return type. Remember your little town of Knowhere? It is time to make a function to improve your town. You are ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.