Learn
Functions
Variadic Parameters

Another parameter supported by Swift is the variadic parameter. A variadic parameter is a parameter that accepts zero or more values of a certain type. A variadic parameter is useful for cases in which we may need to pass in more than one value for a single parameter.

A variadic parameter is denoted by a name, type, and three consecutive dots ... in the function declaration. It’s important to note that a Swift function cannot have more than one variadic parameter.

func functionName(paramName: paramType...) -> returnType {
  // function's task goes here
}

The values passed for a variadic parameter are interpreted as an array of a specific type within the function’s body. Let’s see an example of a variadic parameter in action.

The code below utilizes a variadic parameter in a function to calculate the average exchange rate between a U.S. Dollar and English Pound:

func avgExchangeRate(numbers: Double...) -> Double { var total: Double = 0 let numValues = Double(numbers.count) for number in numbers { total += number } return total / numValues }

In the code above:

  • numbers is a variadic parameter that accepts zero or more values of the type, Double. It is interpreted as an Array within the function.
  • A for-in loop is used to iterate over each number in numbers and add the value of number to the total each time.
  • After the for-in loop completes iterating, the average is calculated by dividing the total by the number of items, numValues, in numbers.

We can invoke the function with the following exchange rates recorded for 6 days in February 2020:

print(avgExchangeRate(numbers: 0.81380, 0.77268, 0.77453, 0.75466, 0.75729, 0.78241)) // Prints: 0.7758949999999999

Each decimal gets passed into the function body and iterated over in the for-in loop. Here’s a breakdown of the values:

Variable Value
total 4.65537
numValues 6.0
total / numValues 0.7758949999999999

Instructions

1.

In the code editor, we’ll write a function, avgSongLength(), that will calculate the average duration of 10 Eurovision songs from the 2019 contest in Israel.

  • The function should accept a times variadic parameter of the type, Int.
  • The function should return a value of the type, Int.

Note: You will see an error in the terminal on the right, but it will go away in the next step when we add code to the body of the function.

2.

Within the body of the function, we will set up a for-in loop to iterate over each element in times and determine the average.

First, declare a variable, total and assign it 0.

Below the variable, set up a for-in loop that will iterate over each item in times.

  • Name the placeholder, time
  • Within the body of the loop, use += to add and reassign the value of time to total.

Below the for-in loop, return the following expression:

total / times.count
3.

Finally, call the function with the following values for times:

183, 176, 180, 176, 184, 179, 181, 180, 172, 178.

These values represent the duration of the first ten songs in the album Eurovision Song Contest: Tel Aviv 2019.

Wrap the function call in a print() statement.

Folder Icon

Take this course for free

Already have an account?