O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Returning values from functions

Swift functions can return a value, tuple, closure, or another function. The ability of a function to return a closure or function is an essential concept for FP as it empowers us to compose with functions. We will get into the use of it when we talk about first-class and higher-order functions.

Syntax-wise, we can specify that a function returns by providing ReturnType after ->. For instance, the following example returns String:

func functionName() -> String { } 

Any function that has ReturnType in its definition should have a return keyword with the matching type in its body.

return types can be optionals in Swift, so the function becomes as follows for our previous example:

 func functionName() -> String? ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required