
- Swift 教程
- Swift - 首頁
- Swift - 概述
- Swift - 環境
- Swift - 基本語法
- Swift - 變數
- Swift - 常量
- Swift - 字面量
- Swift - 註釋
- Swift 運算子
- Swift - 運算子
- Swift - 算術運算子
- Swift - 比較運算子
- Swift - 邏輯運算子
- Swift - 賦值運算子
- Swift - 位運算子
- Swift - 其他運算子
- Swift 高階運算子
- Swift - 運算子過載
- Swift - 算術溢位運算子
- Swift - 恆等運算子
- Swift - 範圍運算子
- Swift 資料型別
- Swift - 資料型別
- Swift - 整數
- Swift - 浮點數
- Swift - Double
- Swift - 布林值
- Swift - 字串
- Swift - 字元
- Swift - 類型別名
- Swift - 可選型別
- Swift - 元組
- Swift - 斷言和前提條件
- Swift 控制流
- Swift - 決策
- Swift - if 語句
- Swift - if...else if...else 語句
- Swift - if-else 語句
- Swift - 巢狀 if 語句
- Swift - switch 語句
- Swift - 迴圈
- Swift - for in 迴圈
- Swift - while 迴圈
- Swift - repeat...while 迴圈
- Swift - continue 語句
- Swift - break 語句
- Swift -貫穿語句
- Swift 集合
- Swift - 陣列
- Swift - 集合
- Swift - 字典
- Swift 函式
- Swift - 函式
- Swift - 巢狀函式
- Swift - 函式過載
- Swift - 遞迴
- Swift - 高階函式
- Swift 閉包
- Swift - 閉包
- Swift - 轉義和非轉義閉包
- Swift - 自動閉包
- Swift 面向物件程式設計
- Swift - 列舉
- Swift - 結構體
- Swift - 類
- Swift - 屬性
- Swift - 方法
- Swift - 下標
- Swift - 繼承
- Swift - 重寫
- Swift - 初始化
- Swift - 析構
- Swift 高階特性
- Swift - ARC 概述
- Swift - 可選鏈
- Swift - 錯誤處理
- Swift - 併發
- Swift - 型別轉換
- Swift - 巢狀型別
- Swift - 擴充套件
- Swift - 協議
- Swift - 泛型
- Swift - 訪問控制
- Swift - 函式與方法
- Swift - SwiftyJSON
- Swift - 單例類
- Swift 隨機數
- Swift 不透明型別和裝箱型別
- Swift 有用資源
- Swift - 線上編譯
- Swift - 快速指南
- Swift - 有用資源
- Swift - 討論
Swift - 巢狀函式
函式是一組語句塊,用於執行特定任務。當一個函式定義在另一個函式內部時,這種型別的函式被稱為巢狀函式。巢狀函式的作用域僅限於其定義所在的函式,這意味著它們只能在其外部函式內部訪問。如果嘗試在外部函式之外訪問它們,則會報錯。
在 Swift 中,單個函式可以包含多個巢狀函式,並且必須在呼叫它們之前定義它們。
巢狀函式提高了可讀性,並透過將複雜任務分解成更小、可重用的元件來組織這些任務。
巢狀函式將邏輯封裝在有限的作用域內,並可以防止外部訪問。
它們只能訪問其外部函式的變數。
巢狀函式的語法
以下是巢狀函式的語法:
func outerFunction(){ func nestedFunction(){ // code } func nestedFunction2(){ //code } nestedFunction1() nestedFunction2() }
Swift 中無引數的巢狀函式
在 Swift 中,可以定義沒有任何引數的巢狀函式。這種型別的函式在呼叫時不接受任何引數,並且可能返回或不返回值。
語法
以下是無引數列表的巢狀函式的語法:
func nestedFunction(){ // Code }
示例
使用巢狀函式顯示世界盃冠軍名稱的 Swift 程式。
import Foundation // Outer function func worldCupTrophy(){ // Nested function without parameter list func winnerName() { print("Winner of World Cup is INDIA") } // Calling nested function winnerName() } // Calling the outer function to display the result worldCupTrophy()
輸出
它將產生以下輸出:
Winner of World Cup is INDIA
Swift 中帶有引數的巢狀函式
巢狀函式可以包含引數列表而沒有返回型別。這種型別的巢狀函式只能訪問外部函式的變數和引數。巢狀函式可以包含多個引數,它們由引數標籤分隔。
語法
以下是帶有引數的巢狀函式的語法:
func nestedFunction(name1: Int, name2: Int){ // code }
示例
使用巢狀函式新增兩個字串的 Swift 程式。
import Foundation // Outer function func concatenateStrings(str1: String, str2: String){ // Nested function with parameter list func displayResult(finalStr: String) { print("Concatenated String: \(finalStr)") } let result = str1 + str2 // Calling nested function displayResult(finalStr: result) } // Input strings var string1 = "Welcome " var string2 = "TutorialsPoint" // Calling the outer function to display the result concatenateStrings(str1: string1, str2: string2)
輸出
它將產生以下輸出:
Concatenated String: Welcome TutorialsPoint
Swift 中無返回型別的巢狀函式
無返回型別的巢狀函式不返回值。它可能包含或不包含引數。
語法
以下是無返回型別的巢狀函式的語法:
func nestedFunction(name: Int){ // code return }
示例
使用巢狀函式計算矩形面積的 Swift 程式。
import Foundation // Outer function func Rectangle(length: Int, width: Int){ // Nested function without return type func displayArea(finalArea: Int) { print("Area of Rectangle: \(finalArea)") } let result = length * width // Calling nested function displayArea(finalArea: result) } // Input strings var l = 10 var w = 12 // Calling the outer function to display the result Rectangle(length: l, width: w)
輸出
它將產生以下輸出:
Area of Rectangle: 120
Swift 中有返回型別的巢狀函式
巢狀函式可以包含引數列表而沒有返回型別。有返回型別的巢狀函式總是返回某些值。
語法
以下是帶有返回型別的巢狀函式的語法:
func nestedFunction(name1: Int, name2: Int) -> ReturnType{ // code return }
示例
使用巢狀函式計算兩個圖形(矩形和正方形)的總面積的 Swift 程式。
import Foundation // Outer function without return type func Areas(length: Int, width: Int){ // Nested function with return type func sumOfAreas(area1: Int, area2: Int)-> Int { let Sum = area1 + area2 return Sum } let areaOfRectangle = length * width let areaOfSquare = length * length // Calling nested function print("Total Area: \(sumOfAreas(area1: areaOfRectangle, area2: areaOfSquare))") } // Calling the outer function to display the result Areas(length: 23, width: 25)
輸出
它將產生以下輸出:
Total Area: 1104
廣告