April 2026
Intermediate
683 pages
25h 29m
English
In Swift, variadic parameters allow a function to accept a variable number of input parameters of the same type. This is a useful feature for cases in which you don’t know how many values will be passed to the function.
As a simple demonstration, Listing 5.36 features the sumNumbers function, which accepts the numbers variadic parameter. If you look closely at the parameter definition, numbers: Int..., you will see that it ends with three dots after the data type. Those dots mark the parameter as variadic, indicating that multiple integers can be sent.
// Functionfunc sumNumbers(_ numbers: Int...) -> Int { return numbers.reduce(0, +)}// Main flowlet total1 = sumNumbers(4) // 4let total2 = sumNumbers(1, 2, 3, 4) // ...
Read now
Unlock full access