swift 基礎-4

來源:互聯網
上載者:User

標籤:

函數:完成特定任務的代碼塊,通過名字來表示函數做什麼

func 函數名(形參:形參類型)->傳回型別

command +option+0 隱藏右邊的框

//定義函數

        func sayHello(name:String)->String{

            let greeting = "hello " + name + "!"

            return greeting

        }

        println(sayHello("Anno"))

        

        // 多個參數

        func minusResult(start:Int,end:Int)->Int{

            return end - start

        }

        println(minusResult(1,10))

        

        //無參數

        func sayHelloWorld() ->String{

            return "Helo World"

        }

        println(sayHelloWorld())

        

        // 無傳回值 Void = 空元組(tuple)

        func sayGoodbye(name:String){

            println("Goodby,\(name)")

        }

        println(sayGoodbye("Dave"))

        // 多重傳回值函數

        func count(str:String) ->(vs:Int,cs:Int,os:Int){

            var vowels = 0,consonants = 0,others = 0

            for Character in str{

                switch String(Character).lowercaseString{

                    case "a","e","i","o","u":

                    ++vowels

                    case "b","c","d","f","g","h","j","k","l","m","n","p","q","t","s","t","v","w","x","y","z":

                    ++consonants

                default:

                    ++others

                }

            }

            return (vowels,consonants,others)

        }

        let total = count("some arbitrary string!")

        println("\(total.vs) vowels and \(total.cs) consonants")

        

        // 外部參數名 把兩個字串連在一起

        func join(s1:String, s2:String,joiner:String) ->String{

            return s1 + joiner + s2

        }

        println(join("hello","world",","))

    // 不使用外部參數的時候,這三個參數不清楚到底是幹什麼的

        func joins(outSting s1:String,toSting s2:String, withJoiner joiner:String) ->String{

            return s1 + joiner + s2

        }

        println(joins(outSting:"hello",toSting:"world",withJoiner:","))

        // 帶來問題:調用函數的時候 寫太多,解決辦法 看下邊

        

        // 簡寫 外部參數名  參數名前 加#

//        func joins1(outSting s1:String,toSting s2:String, withJoiner joiner:String) ->String{

//            return s1 + joiner + s2

//        }

//        println(joins1(outSting:"hello",toSting:"world",withJoiner:","))

        func containsCharacter(#string:String,#characterToFind:Character) ->Bool{

            for character in string{

                if character == characterToFind{

                    return true

                }

            }

            return false

        }

        let containsAVee = containsCharacter(string: "qwertyuiop", characterToFind: "y")

        println(containsAVee)

        

        // 給預設參數

        func joinss(outSting s1:String,toSting s2:String, withJoiner joiner:String = ",") ->String{

            return s1 + joiner + s2

        }

        // 第三個參數 沒寫 直接使用預設值

        let str1 = joinss(outSting: "nihao", toSting: "heri")

        println(str1)

        

        //  注意會出錯 

        func joinbb(outSting s1:String,toSting s2:String, withJoiner joiner:String = "") ->String{

            return s1 + joiner + s2

        }

        // 第三個參數 沒寫 直接使用預設值

        let str2 = joinbb( outSting: "haha", toSting: "zheli", withJoiner: "+")

        println(str2)

        // 可變參數   傳入不確定數量來輸入參數     變數名類型 ...  = 常數陣列

 

swift 基礎-4

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.