- 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
}
推薦閱讀
- 達夢數據庫編程指南
- Java Data Science Cookbook
- Redis應用實例
- 分布式數據庫系統:大數據時代新型數據庫技術(第3版)
- Python廣告數據挖掘與分析實戰
- MySQL從入門到精通(第3版)
- Creating Dynamic UIs with Android Fragments(Second Edition)
- 計算機應用基礎教程上機指導與習題集(微課版)
- INSTANT Android Fragmentation Management How-to
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- MySQL技術內幕:SQL編程
- 數據分析師養成寶典
- Oracle高性能SQL引擎剖析:SQL優化與調優機制詳解
- 數據庫查詢優化器的藝術:原理解析與SQL性能優化
- 數據指標體系:構建方法與應用實踐