美文网首页
Swift 函数

Swift 函数

作者: CaptainRoy | 来源:发表于2018-08-06 14:55 被阅读8次
    • 无参数,无返回值
    func sayHelloWorld() {
        print("Hello World")
    }
    sayHelloWorld() // Hello World
    
    • 参数 返回值
    func greet(person:String)->String {
        return "Hello," + person + "!"
    }
    print(greet(person: "Roy")) // Hello,Roy!
    
    • 多个参数
    func introduce(name:String,age:Int,gender:String) {
        print("我的名字是: \(name) ,年龄: \(age),性别: \(gender)")
    }
    introduce(name: "Roy", age: 18, gender: "男")
    
    • 返回多个值
      得到数组的最大最小值
    func getMinandMaxValue(array:[Int])->(min:Int,max:Int) {
        var currentMin = array[0]
        var currentMax = array[0]
        for value in array {
            if value < currentMin {
                currentMin = value
            } else if value > currentMax {
                currentMax = value
            }
        }
        return (currentMin,currentMax)
    }
    let values = getMinandMaxValue(array: [8, -6, 2, 109, 3, 71])
    print(values.min,values.max)
    
    • 默认值
    func introduce(name:String,age:Int,gender:String = "男") {
        print("名字: \(name),年龄: \(age),性别: \(gender)")
    }
    introduce(name: "Roy", age:18 )
    

    相关文章

      网友评论

          本文标题:Swift 函数

          本文链接:https://www.haomeiwen.com/subject/jaywvftx.html