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

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
}
主站蜘蛛池模板: 专栏| 靖安县| 涿鹿县| 湘乡市| 维西| 黑龙江省| 威信县| 徐水县| 双流县| 华蓥市| 蒙自县| 颍上县| 宜章县| 玉林市| 普格县| 巴林左旗| 金寨县| 康乐县| 五常市| 汪清县| 当涂县| 襄汾县| 肃宁县| 巴里| 武安市| 富源县| 高淳县| 锡林郭勒盟| 浦江县| 霸州市| 青岛市| 宽城| 郴州市| 师宗县| 清涧县| 北京市| 廊坊市| 正镶白旗| 那曲县| 堆龙德庆县| 泰州市|