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

Defining and using variadic functions

Swift enables us to define functions with variadic parameters. A variadic parameter accepts zero or more values of a specified type. Variadic parameters are similar to array parameters but they are more readable and can only be used as the last parameter in multi-parameter functions.

As variadic parameters can accept zero values, we will need to check whether they are empty.

The following example presents a function with variadic parameters of the String type:

func greet(names: String...) {     for name in names {         print("Greetings, \(name)")     } } // To call this function greet(names: "Josee", "Jorge") // prints twice greet(names: "Josee ", "Jorge ", "Marcio") // prints three times 

The most boring part ...

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