Using Variadic Parameters in Swift

Updated on October 16th, 2021

⏱ Reading Time: 3 mins

When coding in Swift, there is a function that all developers undeniably call generously and without much thinking, and that is print. We all use it every day, everywhere; and most of the times, it’s our path towards fast and dirty debugging.

Even though print seems to be winning the battle among the most used commands in Swift, have you ever wondered how it makes it possible to accept an arbitrary number of arguments? And even more, have you ever wanted to implement our own functions that would be acting just like print?

If so, then the answer to all that is simple and has a name; variadic parameters.

Functions with variadic parameters

A function with a variadic parameter is a function with a parameter that can accept zero, one, or more arguments of the same type. To indicate that a parameter is variadic, all we have to do is to append three dots after the parameter’s data type.

Using the value of a variadic parameter is also pretty straightforward inside a function’s body; we treat it as an array. Take a look at the following example that showcases all that:

createSentence(_:) is a simple function that creates a sentence using the words given as arguments. That sentence can be as short or as long as we want, and its length depends on the number of provided words. For instance:

The following function is similar to the previous one; the only thing that changes is the parameter, which this time is an array of String values:

There are two differences when it comes to using the above, comparing always to the first function with the variadic parameter. The first one regards the way we pass values to the function; it has to be an array, in contrast to the previous example where values were separated simply with the comma symbol:

The second difference has already been mentioned; it’s that the function with the variadic parameter can accept zero arguments. The following is perfectly correct as variadic parameters allow to do so:

empty is going to be an empty String in this case. In the second function, we can still avoid providing any values; yet, it remains mandatory to pass an empty array:

Here is a different example of a function with a variadic parameter:

What the above does, is to calculate the average temperature of all those given as arguments. Since temperatures is a variadic parameter, we can supply as many temperature values as we desire, even none. In the latter case, the returned value is just zero:

Note that having a variadic parameter can exist with other normal parameters as well. As an example, see the following function that stores an athlete’s scores in various sport competitions; the athlete’s name is expected to be given as well:

Having multiple variadic parameters

Up until Swift 5.4, a function could contain just one variadic parameter. However, this has changed since version 5.4, and functions may have multiple such parameters.

There is just a requirement necessary to keep in mind, and it became obvious in the previous example; the next parameter after a variadic one should have an argument label, so it’s clear to the compiler which arguments are given to the variadic and which to the next parameter.

The following code demonstrates a function that contains three variadic parameters; note that normal parameters can always get into the mix too:

Summary

Variadic parameters is an easy topic in Swift, and a tool that worths considering when it comes to function parameters. If you are implementing a function or method with parameters that can accept zero or more values, then think about using variadic parameters; you’ll end up with a bit more readable and clearer code, and you keep the advantages of array handling. In any case, I hope you found this post interesting; thank you for reading!

This post has also been published on Medium.

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.