Declare a function with variadic parameters in Swift

In this short Swift code example we 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.

  • Declare a function with variadic parameters
  • Call function with variadic parameter by passing in two value
  • Call function with variadic parameter by passing in three value
  
 // 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")



Learn iOS Development with these Video Courses


How to Make a Freaking iPhone App - iOS 10 and Swift 3
icon icon


iOS 10 & Swift 3: From Beginner to Paid Professional
icon


The Complete iOS 10 Developer Course - Build 21 Apps
icon


Swift 3 - Learn to Code with Apple's New Language
icon icon


Learn How to Build Mobile Apps for iOS with Swift, PHP and MySQL
icon icon

Follow me on one of your favourite social networks to learn about new video tutorials and code examples:

Twitter: @SwiftVideoBlog
Google Plus: https://plus.google.com/+SergeyKargopolov/posts
Facebook: Swift Developer Blog on Facebook