码迷,mamicode.com
首页 > 编程语言 > 详细

swift(五)swift的函数

时间:2015-07-10 13:06:00      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

/**
*  函数的定义和调用
*/

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,width: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)")


/**
*  变参函数
*/

 

swift(五)swift的函数

标签:

原文地址:http://www.cnblogs.com/keyan1102/p/4635277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!