Let Swift organise an Array for you using Variadic parameters

Wouldn’t it be great if Swift could help you organise those function signatures? Automatically?

Although this is a rather basic function within the Swift language it can be quite difficult to get used to using Variadic parameters. Worse is the pronunciation. Still, it is worth knowing about these parameters when creating your own code in Swift.

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Have some rudimentary understanding of functions (guide HERE), and arrays (guide HERE) and possibly inout parameters (guide HERE)

Terminology

Array: An ordered series of objects which are the same type

Variadric parameters: A parameter for a function that accepts zero or more values of the specified type

The syntax

A variadric function uses three periods ... to represent zero or more input variables of a given type

func printNums(items: Int...) {
for item in items {
print (item)
}
}
printNums() // presents no output, as no items are specified
printNums(items: 1, 2) // outputs 1 and 2 to the console.

We can have a nil argument label, illustrated here with a Float input to print a list of our expenditure for a month.

func printExp(_ items: Float...) {
for item in items {
print (item)
}
}
printExp() // presents no output, as there are no items
printExp(1.2, 2.3) // outputs 1.2 and 2.3 to the console.

Now we can follow this up with some real-life examples of the use of variadic functions

Real-life examples and comparison

The comparison

A simple example here will involve printing items from a shopping list, and we can compare using a variadic function to simply passing an array to a function.

Our variadic function γ1 will output the contents of the shopping list to the console.

Similarly, a function γ2 will do the same, taking an array of String and output the shopping list to the console.

Image for post
Image for post

The variadic function

So let us look at an upsetting example that might make you want to cry: you have a program that creates a shopping list, and

func shoppingList(items: String...) {
for item in items {
print (item)
}
}
shoppingList(items: "milk", "eggs")

Awesome! This prints

milk
eggs

if you don’t add any items, that is you call the function shoppingList with

shoppingList()

nothing is printed, since items are empty in this case.

Passing an array

In a world without variadic parameters we can simply use arrays. In this example, we would pass an array of String.

func shoppingList(items: [String]) {
for item in items {
print (item)
}
}
shoppingList(items: "milk", "eggs")

which, of course, prints

milk
eggs

however to call the function without any items we would need the following:

shoppingList(items: [])

Explanation

We can understand that our variadic function has an input of zero or more parameters of the specified type is converted to an array while inside the function.

A function, similarly, has an input of an array. By definition that array

This reasoning also helps us to understand why variadic parameters are used in rather than the similar array parameter — the variadic parameter can accept zero values of the type.

Limitations

There is no such thing as a default variadic parameter. This is perhaps best understood by thinking that our input of zero or more parameters of the specified type is converted to an array while inside the function.

This reasoning also helps us to understand why variadic parameters are used in rather than the similar array parameter — the variadic parameter can accept zero values of the type.

So now, what if you want to add prices to this program?

The following function signature is invalid as functions can only have at most one varidraic parameter:

func shoppingList(items: String..., prices: Float...)

leaving us with limited options.

The best would probably to be to pass a struct called Items which itself contains items and prices as the varidraic parameter:

struct Items {
var item: String
var price: Float
}
func shoppingList(items: [Items]){
for item in items {
print (item.item, item.price)
}
}
shoppingList(items: [Items(item: "milk", price: 1.0), Items(item: "eggs", price: 1.4)]

Now clearly this is the way to go for this type of data. However, we should be aware that the alternative is (as above) using an array for the same usage — that is that we can have zero or more items — whereas with the array alternative to call the function without any items we would need to pass the empty array again [].

The last limitation is that inout parameters cannot be used on varidraic parameters, which means

Conclusion

We should bear in mind that there is no absolutely correct method for all types of data and situations you might encounter. However it is a tool that sits within our wider toolbox that can be used for different situations.

Want to get in contact? Try the link here:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store