Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use Variadic Parameters in Swift
Written by Team Kodeco

A variadic parameter is a function parameter that accepts zero or more values of a specified type. In Swift, you can create a variadic parameter by using three dots (...) after the parameter type. For example:

func printNumbers(numbers: Int...) {
  for number in numbers {
    print(number)
  }
}

In this example, the printNumbers function takes a variadic parameter numbers of type Int.

You can call this function with any number of integers as arguments, for example:

printNumbers(numbers: 1, 2, 3) // prints 1, 2, 3
printNumbers(numbers: 4, 5, 6, 7) // prints 4, 5, 6, 7

Keep in mind that a function can only have one variadic parameter and it must be the last parameter in the function signature.

© 2024 Kodeco Inc.