variadic parameters swift code example

Example 1: swift function in a variable

var pendingFunction: ((Double, Double) -> Double)

Example 2: variadic parameters swift

// This function will take multiple `Int` parameters and add them.
// A variadic parameter in a function is denoted by `<TYPE>...`.
// Note that a function can only have 1 variadic parameter unless
// you use Swift 5.4+, with which you can use multiple.
func add(_ addends: Int...) -> Int {
    var sum = 0

    for num in addends {
        sum += num
    }
    
    return sum 
}

print(add(1, 2, 3, 4)) 				// 10
print(add(5, 2, 3, 4, 6, 7, 8, 10)) // 45