In Swift, variadic functions accept a variable number of arguments. Three dots, ...
, represent the variable arguments. There can only be one variadic parameter in the function’s parameter/argument list.
The syntax to specify a variadic parameter of a function is as follows:
public func functionName(parameterName:dataType...)
func educativeAuthors(_ name: String...)
The above function educativeAuthors
accepts a variable number of strings as a parameter called name.
The method takes the values provided to a variadic argument available as an array of the appropriate type.
For example, in the above function educativeAuthors
, the variadic parameter name
of type String...
is made available within the function’s body as a constant array called name
of type [String].
Refer to the following code to access the variadic parameter values in the function body.
public func educativeAuthors(_ param1:Double, _ names:String...) -> () {for author in names {print("\(author)")}}educativeAuthors(2.4, "john", "sam", "celeste", "gaby")
educativeAuthors
that accepts a variable number of strings as a parameter called names
.
names
array.educativeAuthors
function.