Answers for "varable parameters swift"

0

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
Posted by: Guest on December-31-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language