成都网站建设设计

将想法与焦点和您一起共享

Swift函数基本概念

函数语法:

创新互联建站主营乐至网站建设的网络公司,主营网站建设方案,app开发定制,乐至h5微信小程序开发搭建,乐至网站营销推广欢迎乐至等地区企业咨询

    func 函数名(参数列表)->返回值类型{

        语句组

        return 返回值

}

参数列表:  参数名:类型

                外部参数 ;            

         func rectangleArea(W width:Double,H height:Double) -> Double {

                let area = width * height

                return area 

}

            println("\(rectangleArea(W: 320,H: 480)")

--------------------------------------------------------------------------------------

                

func rectangleArea(# width:Double,# height:Double) -> Double {

                let area = width * height

                return area 

}

            println("\(rectangleArea(width: 320,height: 480)")

--------------------------------------------------------------------------------------------

                

                 参数默认值:在定义函数的时候可以为参数设置一个默认的参数值,当调用函数的时候可以直接忽略该参数:

                

                func makecoffee(type : String = "卡布奇诺") -> String {

                    return "制作一杯\(type)咖啡"

}

            //当参数有默认参数值的时候,在调用参数的时候,需要指定参数名

            let coffee1 = makecoffee(type: "拿铁");

            let coffee2 = makecoffee();

            

            使用下划线指定外部参数名,调用参数的时候,可以省略参数名,前提是参数有默认值:

            func circleArea (R radius : Double = 30, _ pi : Double = 3.14){

                    let area = radius * radius * pi

                    return area;

}

                println("circle area:\(circleArea(R: 50,3.14159))")

---------------------------------------------------------------------------------------------

                

                可变参数;

            函数参数个数可以变化,它可以接受不固定数量的输入类型参数,它们具有相同的类型,有点像是传递数组,可以在参数类型后面加入(...)方式来指明是可变参数

                    示例:

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

                        var total : Double = 0

                        for number in numbers {

                        total += number

}

                    return total

}    

                  sum(10,20,30),sum(10,50)

                参数的传递引用:

                        参数传递的方式有2种:值类型和引用类型

                             只有类是引用类型,其他数据类型如整型,浮点型,布尔型,字符串,元组,结合,枚举和结构体全部是值类型

                示例:

            class PassTest (

                var ptValue = 0 

)

            func changeObValue(ref : PassTest) {

                    ref.ptValue = 99

}

                                

            var pt = PassTest()

            println(pt.value)

            

             changeObValue(pt)

             println(pt.value)                    

            func changeInt(var value : Int) {

                value = 55

}

                var val = 11

            changeInt(val)

                

            println(val)

            如果一定要将一个值类型参数作为引用传递,也是可以实现的,Swift提供的inout关键字就可以实现

            

                

           func changeInt(inout value : Int) {

                value = 55

}

                var val = 11

            changeInt(&val)

                

            println(val)

    

需要了解的问题:

1、关于单值和多值的问题;

2、关于无法返回值的情况;

实例:

    func rectangleArea(width:Double,height:Double) -> Double {

                let area = width * height

                return area 

}

println("\(rectangleArea(320,480)")


分享标题:Swift函数基本概念
本文URL:http://chengdu.cdxwcx.cn/article/pphgcd.html