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

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
}
主站蜘蛛池模板: 三江| 蒙山县| 伊川县| 汉川市| 哈尔滨市| 阜新市| 和田县| 南昌县| 高台县| 丽江市| 惠安县| 和政县| 镇赉县| 吉林省| 白玉县| 高阳县| 阿拉尔市| 濮阳市| 安西县| 屏东县| 梨树县| 大渡口区| 肥西县| 红河县| 昆明市| 张家川| 敦化市| 玉环县| 巫溪县| 田东县| 鄄城县| 隆安县| 象山县| 南皮县| 马关县| 蕉岭县| 庆云县| 方山县| 贡觉县| 治县。| 安达市|