/** * 函数的定义和调用 */ func showIntegerArray(array:[Int]) { for a in array { println("(a)") } } showIntegerArray([1,2,3]) func maxOfValue(a:Int,b:Int) ->Int { return a > b ? a : b } println(maxOfValue(3,5)) func showHelloWord(){ println("Hello World") } showHelloWord() let p0:(x:Double,y:Double) = (0,0) let p1:(x:Double,y:Double) = (6,6) func getLengthAndWidth(p0:(x:Double,y:Double),p1:(x:Double,y:Double)) -> (length: Double,Double){ return (abs(p0.1-p1.1),abs((p0.0-p1.0))) } let w = getLengthAndWidth(p0, p1).width let len = getLengthAndWidth(p0, p1).length println("(len):(w)") /** * 函数的内部参数和外部参数 */ //func divisionOpertation(dividend a:Double,divsior b:Double) -> (Double) //{ // return a / b //} //加#代表内外参数 func divisionOpertation(#dividend :Double,#divsior :Double) -> (Double) { return dividend / divsior } let res = divisionOpertation(dividend: 3.5, divsior: 1.2) println(res) /** * 函数的默认参数 */ //func joinString(s1: String,toString s2:String, joiner s3:String = "#")->(String) //{ // return s1+s3+s2 // // // //} //默认参数可以放到任意位置 func joinString(s1: String,toString s2:String, joiner :String = "?")->(String) { return s1+joiner+s2 } let str = joinString("hello", toString: "world" ,joiner:"#") println(str) /** * 常量参数和变量参数 */ //默认常量不能修改可以起到保护作用 //func swap(var a:Int,var b:Int) //{ // let t = a // a = b // b = t //} func swap(inout a:Int,inout b:Int) { let t = a a = b b = t } /** * 输入输出函数(inout) */ var x = 10 var y = 19 swap(&x, &y) println("(x),(y)") /** * 变参函数 */