官术网_书友最值得收藏!

Defining and using nested functions

In Swift, it is possible to define functions inside other functions. In other words, we can nest functions inside other functions. Nested functions are only accessible inside their enclosing functions and are hidden from the outside world by default. The enclosing function can return the nested function in order to allow the nested function to be used in other scopes. The following example presents a function that contains two nested functions and returns one of them according to the value of its isPlus parameter:

func choosePlusMinus(isPlus: Bool) -> (Int, Int) -> Int { 
func plus(a: Int, b: Int) -> Int {
return a + b
}
func minus(a: Int, b: Int) -> Int {
return a - b
}
return isPlus ? plus : minus
}
主站蜘蛛池模板: 当涂县| 色达县| 佛冈县| 沈阳市| 靖边县| 尼木县| 红原县| 惠安县| 兴国县| 中山市| 鄂托克旗| 金乡县| 鄄城县| 五原县| 永福县| 汝城县| 滨海县| 丹凤县| 通许县| 科尔| 宿州市| 大荔县| 洪湖市| 永川市| 昭苏县| 荥经县| 盐源县| 兴国县| 连江县| 禄劝| 富裕县| 拉萨市| 雷山县| 蒲城县| 安阳市| 双流县| 乳山市| 郸城县| 衡山县| 威远县| 彭州市|