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

First-class functions

In the Function types section of this chapter, we saw that we can define function types and store and pass functions around. In practice, this means that Swift treats functions as values. To explain this, we will need to examine a couple of examples:

let name: String = "Grace" 

In this code example, we create a constant of the String type name and store a value ("Grace") into it.

When we define a function, we need to specify the type of parameter, as follows:

func sayHello(name: String) {     print("Hello, \(name)") } 

In this example, our name parameter is of the String type. This parameter could be any other value type or reference type. Simply, it could be Int, Double, Dictionary, Array, Set, or it could be an object ...

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