Variadic functions — swift

Variadic functions in Swift are functions that can accept a variable number of arguments of the same type. This feature enhances the versatility and readability of Swift code.

Abhimuralidharan
2 min readJul 7, 2017
Photo by Lala Azizli on Unsplash

In computer programming, a variadic function is a function which accepts a variable number of arguments. The function arguments are represented by (three period characters) after the argument’s type that can be accessed into their body as an array .

You might have used a print statement in swift. It looks like this:

print(“apple”,”pineapple”,”orange”)

This is a variadic function which accepts parameter of type Any .

According to apple docs: The values passed to a variadic parameter are made available within the function’s body as an array of the appropriate type. For example, a variadic parameter with a name of numbers and a type of Double... is made available within the function’s body as a constant array called numbers of type [Double].

Playground code [Example] :

func printFruitNames (_ fruits: String...) -> () {for fruit in fruits { // fruits is a string arrayprint(“\(fruit)”)

--

--