Closures with one parameter in Swift
//explicitly specify return type
let powOfTwo = {
(power: Int) -> Int in Int(pow(2.0, Double(power)))
}
let pow8 = powOfTwo(8)
//pow8 is 256
//implicitly specified return type
let powOfThree = {
power in pow(3.0, power)
}
let pow3 = powOfThree(3)
//pow3 is 27.0
- Log in to post comments