Function Types

Each function you have been working with in this chapter has a specific type. In fact, all functions do. Function types are made up of the function’s parameter and return types. Consider the sortEvenOdd(_:) function. This function takes an array of integers as an argument and returns a tuple with two arrays of integers. Thus, the function type for sortEvenOdd(_:) is expressed as: ([Int]) -> ([Int], [Int]).

The function’s parameters are listed inside the left parentheses, and the return type comes after the ->. You can read this function type as: A function with one parameter that takes an array of integers and returns a tuple with two arrays containing integers. For comparison, a function with no arguments and no return ...

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.