​Quick is a powerful and user-friendly development language for macOS, iOS, watchOS and tvOS....

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


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.