crashoverride777 - 1 year ago 76
Swift Question

# Swift - What does ... mean in a function parameter?

What does the ... syntax mean in function parameters

For example

``````func setupViews(views: UIView...) {
...
}
``````

I saw this in some tutorial recently and as far as I understand its just an array of UIViews.

So is it the same as writing

``````func setupViews(views: [UIView]) {
...
}
``````

or is there a difference?

It represents a Variadic Paramater, from the docs:

A variadic parameter accepts zero or more values of a specified type. You use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called. Write variadic parameters by inserting three period characters (`...`) after the parameter’s type name.

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]`.

The example below calculates the arithmetic mean (also known as the average) for a list of numbers of any length:

``````func arithmeticMean(numbers: Double...) -> Double {
var total: Double = 0
for number in numbers {
total += number
}
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers
``````

You can only have one Variadic Param per function.

As you can see, there is a subtle difference between an input paramater of `[Double]` and `Double...`

When using a function with a Variadic parameter you don't need to pass the objects/values as an array.

Food for thought; How would you call this method? `func arithmeticMean(numbers: [Double]...) -> Double`

Like so:

``````arithmeticMean([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]) // you could keep adding more and more arrays here if desired.
``````

In this example 'numbers' would be an array of double arrays.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download