func add swift code example
Example 1: swift how to call a function
//function trying to be called
func aFunction() {
print("called function")
}
//call function like this
aFunction()
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