1天學會swift2 0語法(3)函式 和 閉包

liangtongzhuo發表於2017-12-14

######函式

func demo(str: String) {
//沒有返回值
    print(str)
}


//函式的外部引數
//1.能夠讓外界呼叫 對函式的語義更加清晰 
//2.在函式內部 使用 更加簡潔
func area(height a: Int, width b: Int) -> Int {
    return a * b
}

//函式的基本格式
// func 函式名(引數名: 引數型別) -> 返回值型別
func sum(a: Int, b: Int) -> Int {
    return a + b
}
複製程式碼
  • 函式呼叫

      demo("哈哈")
      let result1 = area(height: 10, width: 20)
    複製程式碼

//第一個引數名 可以被省略 let result2 = sum(10, b: 100)

  • 閉包

看我那麼可愛n(≧▽≦)n 關注我的微薄 (樑同桌):http://weibo.com/tongrenyinsheng 個人網站(同人音聲) http://www.tongrenyinsheng.com ios 個人寫的app (同人音聲)ASMR音樂

相關文章