Nested Function
A function defined at the top layer of a source file is called a global function. A function defined in a function body is called a nested function.
In the following example, the nested function nestAdd is defined in the foo function, and therefore nestAdd can be called inside foo, or it can be returned as a return value and called outside foo:
func foo() {
func nestAdd(a: Int64, b: Int64) {
a + b + 3
}
println(nestAdd(1, 2)) // 6
return nestAdd
}
main() {
let f = foo()
let x = f(1, 2)
println("result: ${x}")
}
The result is as follows:
6
result: 6