Variadic parameters in Swift allow zero or more values of the same type to be passed into a single parameter in a function. This is indicated by appending ...
to the type annotation of the parameter.
These values will be automatically grouped into an array with elements of the same type as the type of the variadic parameter.
func geometricMean(_ numbers: Double...) -> Double {
var total = 1.0
for number in numbers {
total *= number
}
return pow(total, 1.0 / Double(numbers.count))
}
geometricMean(1, 2, 3, 4, 5)
// => 2.605171084697352
When using variadic parameters, Swift has two limitations: