• 函数的基本用法

一个简单的函数,如下:

     func sayHello(personName:String) -> String {

         let s = "你好" + personName + "!"

         return s

     }

     let s = sayHello("iOS")

     print(s)

     func关键字:用于标示,这是一个函数

     函数名字:sayHello  应当清晰明了且符合驼峰规则编码规范

     参数列表:(personName:String)  personName是参数名,String是这个参数的类型

     返回值:-> String 表示返回值是一个字符串类型

     函数体:{ } 放置代码,即函数的执行代码

  • 函数的参数

函数可以没有参数:

     func sayHello() -> String {

         return "你好!"

     }

     print(sayHello())

可以为函数设置多个参数,如:

     func sayHello(personName personName:String, isMale isMale:Bool) -> String {

         var s:String;

         if ( isMale ) {

             s = "你好" + personName + ",帅哥!"

         }

         else {

             s = "你好" + personName + ",美女!"

         }

         return s

     }

     print(sayHello(personName: "iOS", isMale: true))

参数的名字:

     内部参数名:该参数名只在函数的内部能够看到,如:

     func sayHello(personName:String) -> String {

         let s = "你好" + personName + "!"

         return s

     }

     print(sayHello("iOS"))

     外部参数名:该参数名还可以在函数的外部看到,如:

     func sayHello(personName personName:String) -> String {

         let s = "你好" + personName + "!"

         return s

     }

     print(sayHello(personName: "iOS"))

     第二个参数及后面的参数,无论第一个参数是否是外部参数,都是外部参数,如:

     func sayHello(person1Name:String, person2Name:String) -> String {

         let s = "你好 " + person1Name + " and " + person2Name + ""

         return s

     }

     print(sayHello("iOS", person2Name: "Android"))

     后续的参数前添加 “_ "可以打破这一规则

     func sayHello(person1Name:String, _ person2Name:String) -> String {

         let s = "你好 " + person1Name + " and " + person2Name + ""

         return s

     }

     print(sayHello("iOS", "Android"))

带可变参数的函数,如:

     func sum(numbers: Int ...) -> Int {

         var total = 0;

         for n in numbers {

             total += n

         }

         return total

     }

     print(sum(1,2,3,4,5))

函数的默认参数,如:

     func sayHello(personName personName:String, isMale:Bool true) -> String {

         var s:String;

         if ( isMale ) {

             s = "你好" + personName + ",帅哥!"

         }

         else {

             s = "你好" + personName + ",美女!"

         }

         return s

     }

     //print(sayHello(personName: "iOS", isMale: true))

     print(sayHello(personName: "iOS"))

     函数的默认参数,要遵循从最右边开始的规则

函数的重载,如:

     func sayHello(personName personName:String) ->String {

         let s = "你好" + personName + "!"

         return s

     }

     func sayHello(personName personName:String, isMale:Bool) -> String {

         var s:String

         if ( isMale ) {

             s = "你好" + personName + ",帅哥!"

         }

         else {

             s = "你好" + personName + ",美女!"

         }

         return s

     }

     print(sayHello(personName: "iPhone”))  // 你好iPhone!

     print(sayHello(personName: "iOS", isMale: true)) // 你好iOS,帅哥!

     函数重载的依据是:参数列表不同

     但要注意函数在重载时,如果参数带默认值带来的歧义的情况

参数的可变性:

     参数默认都是常量(let),在函数内不能进行修改

     可以显式声明参数为变量(var),这样参数在函数内就可以被修改

     func factorial(var number:Int) ->Int {

         var result:Int = 1

         while number > 1 {

             result = result * number

             number--

         }

         return result

     }

     print(factorial(6))

     要注意:形参不能修改实参

形参修改实参的办法:

     swift精心设计了inout关键字,用于声明形参是一个出参,实参使用&运算符

     func mySwap(inout a:Int, inout b:Int) {

         var tem:Int

         tem = a

         a = b

         b = tem

     }

     var x1 = 10, x2 = 20

     mySwap(&x1, b: &x2)

     print("x1=\(x1) x2=\(x2)”)     // x1=20 x2=10

     注意:出参的实参必须是变量、出参不能带默认值、inout关键不能和let及var一期使用

  • 函数的返回值

无返回值:

     func mySwap(inout a:Int, inout b:Int) {

         var tem:Int

         tem = a

         a = b

         b = tem

     }

     var x1 = 10, x2 = 20

     mySwap(&x1, b: &x2)

     print("x1=\(x1) x2=\(x2)”)     // x1=20 x2=10

一个返回值:

     func factorial(var number:Int) ->Int {

         var result:Int = 1

         while number > 1 {

             result = result * number

             number--

         }

         return result

     }

     print(factorial(6))

多个返回值(元组):

     func count(kids: Dictionary<String, String>) -> (girls:Int, boys:Int{

         var girls = 0, boys = 0

         for c in kids.values {

             if c == "Girl" {

                 girls++

             }

             else if c == "Boy" {

                 boys++

             }

         }

         return (girls, boys)

     }

     var k:Dictionary<String, String> = ["AAA":"Girl", "BBB":"Boy", "CCC":"Girl", "DDD":"Girl", "EEE":"Boy"]

     var (g, b) = count(k)

     print("girls=\(g) boys=\(b)")