Page 98

Swift

func sum(a: Int, b: Int) -> Int { return a + b } var addition: (Int, Int) -> Int = sum println("Result: \(addition(40, 89))") func another(addition: (Int, Int) -> Int, a: Int, b: Int) { println("Result: \(addition(a, b))") } another(sum, 10, 20) When we run the above program using playground, we get the following result: Result: 129 Result: 30

Nested Functions A nested function provides the facility to call the outer function by invoking the inside function. func calcDecrement(forDecrement total: Int) -> () -> Int { var overallDecrement = 0 func decrementer() -> Int { overallDecrement -= total return overallDecrement } return decrementer } let decrem = calcDecrement(forDecrement: 30) println(decrem()) When we run the above program using playground, we get the following result: -30

86

Swift is a powerful and instinctive programming language for macOS, iOS, watchOS and tvOS....  

...

Advertisement