Swift學(xué)習(xí)(五:函數(shù))

1.函數(shù)的定義與調(diào)用
  • 定義
    func say(name:String) -> String{

              return "hello", + name + "!"
      }
    
  • 調(diào)用

      print(say("wanwan"))          //hello,wanwan!
    
  • 無(wú)參函數(shù)

      func say() -> String{
              return "hello,wanwan"
      }
      print(say())        //hello,wanwan
    
  • 多參函數(shù)
    func say(name:String,isgirl:Bool) -> String{
    if isgirl{
    return "hello", + name + "!"
    }else{
    return "hello"
    }

      }
      * 調(diào)用
      print(say("wanwan",ture))                        
      //hello,wanwan!
    
  • 無(wú)返回值函數(shù)

      func say(name:String){
                print("hello,wanwan")                
      }                           
      say("wanwan")
      因?yàn)檫@個(gè)函數(shù)不需要返回值,所以這個(gè)函數(shù)的定義中沒有返回箭頭(->)和返回類型。
    
  • 當(dāng)一個(gè)函數(shù)調(diào)用另一個(gè)函數(shù)的時(shí)候,該函數(shù)返回值可以忽略

    func inputsome(name:String) -> Int {
              print("name\(name)")
              return name.characters.count
    }
    
      func lala(name: String) {
              inputsome(name)
      }
    
  • 多重返回值

      func minmax(array: [Int]) -> (min: Int, max: Int) {
              var currenmin = array[0]
              var currenmax = array[0]
              for value in array[1..<array.count]{
                      if value < currenmin{
                                  currenmin = value
                      }else if value > currenmax{
                                  currenmax = value
                      }
              }
              return (currenmin,currenmax)
      }
        minmax([1,2,3,4,5,6,7,8,8])
    
  • 函數(shù)參數(shù)名稱

      func come(first:String,last:String) {
              print("\(first)\(last)")
      
     }
      come("wanwan", last: "heihei")
    

函數(shù)有內(nèi)部參數(shù)名和外部參數(shù)名,外部參數(shù)省略第一個(gè)參數(shù)名,第二個(gè)及后面的都需要帶上參數(shù)名。

  • 指定外部參數(shù)名

      可以在局部參數(shù)名前指定外部參數(shù)名,中間空哥分開  
      func some(outname inname:String) {
      
      }
      some(outname: "wanwan")
    
     func sayHello(to person: String, and anotherPerson: String) -> String {
            return "Hello \(person) and \(anotherPerson)!"
      }
      print(sayHello(to: "Bill", and: "Ted"))
      // prints "Hello Bill and Ted!"
    
  • 忽略外部參數(shù)名

      func come(firat:Int , last : Int){
              
      }
      come(1,2)
    
  • 默認(rèn)參數(shù)

      func come(firat:Int =12){
             print(firat)
     }
     come()    //12
    
  • 可變參數(shù)

     func some(number:Double...) -> Double {
              var total:Double = 0
              for num in number {
                  total += num
              }
              return total
      }
      some(1,2,3)
    
  • 輸入輸出參數(shù)
    函數(shù)參數(shù)默認(rèn)是常量,不能在函數(shù)內(nèi)部修改參數(shù)值,如果想修改,并且函數(shù)調(diào)用結(jié)束后這些修改仍然存在,就要定義為輸入輸出參數(shù):inout

      func swapTwoInts(inout a: Int, inout _ b: Int) {
          let temporaryA = a
          a = b
          b = temporaryA
      }
    
     var someInt = 3
      var anotherInt = 107
      swapTwoInts(&someInt, &anotherInt)
    print("someInt is now \(someInt), and   anotherInt is now \(anotherInt)")
    // prints "someInt is now 107, and anotherInt is now 3"  
    

注意 參數(shù)名必須用 inout標(biāo)記 調(diào)用必須用指針&

2.函數(shù)類型

每個(gè)函數(shù)都有種特定的函數(shù)類型,由函數(shù)的參數(shù)類型和返回類型組成。

    func addTwoInts(a: Int, _ b: Int) -> Int {
        return a + b
    }
    func multiplyTwoInts(a: Int, _ b: Int) -> Int {
        return a * b
    }
    這兩個(gè)函數(shù)的類型是 (Int, Int) -> Int,可以解讀為“這個(gè)函數(shù)類型有兩個(gè) Int 型的參數(shù)并返回一個(gè) Int 型的值?!?
3.使用函數(shù)類型
  • 定義一個(gè)有兩個(gè)Int類型的參數(shù)并返回一個(gè)Int型的值的函數(shù)變量,并將addTwoInts函數(shù)賦值給它
    func addTwoInts(a: Int, _ b: Int) -> Int {
    return a + b
    }

      var mathadd: (Int, Int) -> Int = addTwoInts
      
      現(xiàn)在可以用 `mathadd`調(diào)用被賦值的函數(shù)
      mathadd(1,4)       //5
    
  • 相同類型的不同函數(shù)可以賦值給同一變量

      func multiplyTwoInts(a: Int, _ b: Int) -> Int {
          return a * b
      }         
    
      mathadd = multiplyTwoInts
      marhadd(2,3)     //6
    
4.函數(shù)類型作為參數(shù)類型
函數(shù)作為參數(shù)類型
    定義一個(gè)`aa`類型的函數(shù),再定義一個(gè)包含`aa`類型函數(shù)的函數(shù),調(diào)用的時(shí)候傳入`aa`類型的函數(shù)。
5.函數(shù)類型作為返回類型

先定義幾個(gè)函數(shù),這兩個(gè)函數(shù)類型都是 (Int) -> Int

func stepfor(input:Int) -> Int {
    return input+1
}

func stepback(input:Int) -> Int {
    return input-1
}

下面這個(gè)函數(shù)的返回類型為(Int) -> Int

func choose(back:Bool) -> (Int)->Int {
    
    return back ? stepfor : stepback
}

調(diào)用:

var value = 3
let over = choose(value >0)    //這時(shí)候的返回結(jié)果應(yīng)該是`stepfor`這個(gè)函數(shù)
over(4)    // 給這個(gè)函數(shù)賦值
print(over(4))    //打印出來(lái)為5

上述步驟其實(shí)可以簡(jiǎn)化
let over = choose(value>0)(4)        //5
6.嵌套函數(shù)

全局函數(shù)可以被外界訪問,嵌套函數(shù)只能被它的外圍函數(shù)調(diào)用,上面的例子可以改寫為嵌套函數(shù):

func choose(back:Bool) -> (Int)->Int {
    
    func stepfor(input:Int) -> Int { return input+1}
    
    func stepback(input:Int) -> Int {return input-1}
    
    return back ? stepfor : stepback
}

調(diào)用:

    var value = 3
    let over = choose(value>0)(4)        //5
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容