Passing lists from one function to another in Swift

The numbers: Int... parameter in sumOf is called a variadic parameter. That means you can pass in a variable number of that type of parameter, and everything you pass in is converted to an array of that type for you to use within the function.

Because of that, the numbers parameter inside average is an array, not a group of parameters like sumOf is expecting.

You might want to overload sumOf to accept either one, like this, so your averaging function can call the appropriate version:

func sumOf(numbers: [Int]) -> Int {
    var sum = 0
    for number in numbers {
        sum += number
    }
    return sum
}
func sumOf(numbers: Int...) -> Int {
    return sumOf(numbers)
}

Leave a Comment