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

Functions

Functions are self-contained blocks of code that perform a specific task.

In Swift, functions are first-class citizens, meaning that they can be stored, passed, and returned. Functions can be curried and defined as higher-order functions that take other functions as their arguments.

Functions in Swift can have multiple input parameters and multiple returns using tuples. Let's look at the following example:

func greet(name: String, day: String) ->String {     return "Hello \(name), today is \(day)" } greet(name: "Francois", day:"Saturday") 

Functions can have variadic parameters. Consider the following example:

// Variable number of arguments in functions - Variadic Parameters func sumOf(numbers: Int...) -> (Int, Int) {  var sum = ...

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