swift2.0笔记2

函数

1. 函数定义

func sayHello(personName: String) -> String{
    let greeting = "Hello, "+personName+"!";
    return greeting;
}
  • 函数参数和返回值

    swift中可以定义任意类型的函数,

    • 无参函数
    • 只带一个参数或多个参数
    • 带有表达性参数名
    • 参数是一个函数
func  sayHello(personName: String,alreadyGreeted: Bool) -> String{
    if (alreadyGreeted)
    {
        print("Hello again,"+personName+"!");
    }else{
        print(sayHello(personName));
    }
}

当调用超过一个参数的函数时,第一个参数后的参数根据其对应的参数名称标记。

print(sayHello("Tim",alreadyGreeted: true));
  • 无返回值函数
func sayGoodbye(personName: String){
    print("Goodbye "+personName)
}

print(sayGoodbye("Dave"));
  • 多重返回值函数
    //在一个Int数组中查找最大值和最小值
    func minMax(array: [Int]) -> (min: Int, max: Int){
        var curentMin = array[0];
        var currentMax = array[0];
        for value in array[1..<array.count]{
            if value < curentMin{
                curentMin = value;
            }else if value > currentMax{
                currentMax = value;
            }
        }
        return (curentMin,currentMax);
    }

    let bounds = minMax([8,-6,,109,3,71]); //bounds是元组
    //元组的成员值已经被命名,所以直接用"."语法访问元组中的值
    print("min is \(bounds.min) and max is \(bounds.max)");
  • 可选元组返回类型
    返回的元组可能没有值,也就是返回的值可能是nil时用下面方式表示

    (String,Int,Bool)?

    func minMax(array: [Int]) -> (min: Int, max: Int)? {
        if array.isEmpty {
            return nil;
        }
        var curentMin = array[0];
        var currentMax = array[0];
        for value in array[1..<array.count]{
            if value < curentMin{
                curentMin = value;
            }else if value > currentMax{
                currentMax = value;
            }
        }
        return (curentMin,currentMax);
    }

    //用if let可选绑定来检查返回的是一个实际的元组还是一个nil 
    if let bounds = minMax([8,-6,,109,3,71]){
        //bounds是元组
        //元组的成员值已经被命名,所以直接用"."语法访问元组中的值
        print("min is \(bounds.min) and max is \(bounds.max)");
    }

2. 函数的参数名称

func someFunction(firstPataeterName: Int, secondParameterName: Int){
        // your code here
    }
    someFunction(1,secondPatameterName: 2); 
  • 特点

    • 外部调用时,第一个参数名可省略

    • 外部调用时,第二个参数及随后的参数必须使用参数名

    • 同一个函数内部参数名不能相同

    • 指定外部参数名

    func sayHello(to personName: String, and anotherPersonName: String) -> String{
        return "Hello "+personName+",and "+anotherPersonName;
    }

    print(sayHello(to: "Mike",and: "Tim"));

    //在调用sayHello()时,两个外部参数名都必须写出来
  • 忽略外部参数名

    调用函数时,如果不想第二个及后续的参数都必须写参数名,在定义函数的时候可用一个(_)代替一个明确的参数名(省略不谢);

    func someFunction(firstPataeterName: Int, _ secondParameterName: Int){
        // your code here
    }
    someFunction(1,2); //忽略了第二个参数名
  • 默认参数值
    func someFunction(defaulValue: Int = 12){
        //your code
    }

    someFunction(6);  //defaulValue = 6
    someFunction();  //defaulValue= 12
  • 可变参数

    • 可以接受0个或者多个参数

    • 定义:在类型后加(…)定义

    • 一个函数只能有一个可变参数

    • 可变参数只能放在参数列表的最后

    // 计算一组任意长度数字的算术平均数
    func arithmeticMean(numbers: Double...) -> Double{
        var total: Double = 0;
        for number in numbers{
            total += numbers;
        }

        return total / Double(numbers.count);
    }
  • 常量参数和变量参数

    • 参数前用var定义,可以当做变量用
    func alignRight(var string: String,totalLength: Int) -> String{
        //string可作为变量使用
    }
  • 输入输出参数

    • 特点;在函数内部被修改时,函数外面的也同时被修改;
    • 定义:在函数参数前加inout关键字;
    • 不能传入常量和字面量,因为其都是不可变的;
    • 在调用函数时,需要在参数前加”&”
    • 相当于java语言中的引用类型
         // 交换两个数的值
        func swapTwoInts(inout a: Int,inout b: Int){
            let temp = a;
            a = b;
            b = temp;
        }
    
        var someInt = 3;
        var anotherInt = 107;
        swapTwoInts(&someInt,&anotherInt);
        print("someInt is now \(someInt),anotherInt is now \(anotherInt)");

3.函数类型

1)定义:由函数的参数类型和返回值类型组成
2) 可将一个函数赋值给一个函数类型的变量
    // 两个函数:
    func addTwoInts(a: Int,_ b: Int) -> Int{
        return a+b;
    }
函数的类型为:(Int,Int) -> Int

 定义一个函数类型的变量:
     var mathFunction: (Int,Int) -> Int = addTwoInts 
     //把一个函数赋值给一个函数类型的变量
     print("结果为:\(mathFunction(2,3))");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值