If you intend to pass an unknown number of parameters to a function (from a logical point of view, you cannot say โan infinite number of parametersโ due to the memory limitations of the device), it is called a variational parameter :
A paradigm parameter accepts zero or more values โโof a given type. You use a variable parameter to indicate that the parameter may be after the function call the number of input values โโhas changed. Write variable parameters by inserting three period characters (...) after the parameter type name.
For example, say you want to implement a function that takes an unknown number of Ints to sum them:
func summationOfInfiniteInts(ints: Int...) -> Int { return ints.reduce(0, +) } let summation = summationOfInfiniteInts(ints: 1, 2, 3, 4)
Note that the ints
parameter in the summationOfInfiniteInts
block is represented as [Int]
(an array of Int
).
Hope this helps.
source share