- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 130字
- 2021-07-02 23:54:32
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
}
推薦閱讀
- 同步:秩序如何從混沌中涌現(xiàn)
- LibGDX Game Development Essentials
- 在你身邊為你設(shè)計(jì)Ⅲ:騰訊服務(wù)設(shè)計(jì)思維與實(shí)戰(zhàn)
- Spark快速大數(shù)據(jù)分析(第2版)
- Voice Application Development for Android
- MySQL從入門(mén)到精通(第3版)
- 數(shù)據(jù)庫(kù)原理與設(shè)計(jì)(第2版)
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- SQL Server 2012實(shí)施與管理實(shí)戰(zhàn)指南
- The Natural Language Processing Workshop
- Spring Boot 2.0 Cookbook(Second Edition)
- Node.js High Performance
- 算法設(shè)計(jì)與分析
- SQL Server 2008寶典(第2版)
- 標(biāo)簽類(lèi)目體系:面向業(yè)務(wù)的數(shù)據(jù)資產(chǎn)設(shè)計(jì)方法論