In this tutorial you will learn about the Swift Variadic Function and its application with practical example.
Swift Variadic Function
A variadic function is a function that can accept variable number of arguments. In Swift, when are you not sure about the input parameter for a function then you have option to create a A variadic function. A variadic function allows us to You can pass zero or more parameters for a function. A variadic function can be declare using an ellipsis/three dots after type of parameter.
Example:-
1 2 3 4 5 6 7 8 9 |
func addnum(_ nums: Int...) -> Int { var sum: Int = 0 for num in nums { sum += num } return sum } print("W3Adda - Swift variadic function") print(addnum(10,11,12,13,14,15)) |
Output:-