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

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
}
主站蜘蛛池模板: 正安县| 新丰县| 玉屏| 荆州市| 涪陵区| 清流县| 凌源市| 日喀则市| 旌德县| 黄浦区| 类乌齐县| 安仁县| 苍山县| 呼和浩特市| 长治市| 云安县| 萨嘎县| 台北县| 重庆市| 东乌| 桦川县| 浦县| 万山特区| 稻城县| 郴州市| 昌邑市| 仲巴县| 抚宁县| 图木舒克市| 凌源市| 临漳县| 宁晋县| 遂昌县| 聊城市| 当雄县| 左云县| 凤庆县| 抚顺市| 珠海市| 蓬莱市| 五家渠市|