Swift Introduction | Function Literals and Closures | Functions Are Values
Swift functions have an important characteristic: they can be treated as values. A function groups together a process, but in Swift the function itself is also a value.
Run the following simple example.
func calc(num:Int)->Int {
var res = 0
for n in 0...num {
res += n
}
return res
}
var f1 = calc
print(f1(10))
This example defines a function named calc. It assigns calc to the variable f1, then calls f1 with an argument.
The calc process runs correctly and returns a result. In other words, the calc function assigned to f1 works as expected.
Because functions are values, Swift can handle functions in a variety of ways. Remember this key point: a function is a value.