Declare a function with variadic parameters in Swift

In this short Swift code example, you will learn how to declare a function that can take variadic parameters. We use a variadic parameter to specify that the parameter can be passed a varying number of input values when the function is called.

The below code example will cover:

  • Declare a function with variadic parameters,
  • Call a function with variadic parameters by passing in two value,
  • Call a function with variadic parameters by passing in three values.

Variadic Parameters Example in Swift

 // Declare a function that can take variadic parameters
func printOutFriendNames(names: String...)  {
  
    for name in names {
 
        print(name)
    }
 
}
// Call the printOutFriendNames with two parameters
printOutFriendNames("Sergey", "Bill")
// Call the function with more parameters
printOutFriendNames("Sergey", "Bill", "Max")

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published. Required fields are marked *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!