美文网首页
学习Swift (第四天)

学习Swift (第四天)

作者: RDPCode | 来源:发表于2016-09-28 22:50 被阅读10次

一、方法 (Function)

方法就是来完成特定功能的代码块。

语法

func funcname (型参) -> 返回值{

return 

}

直接上代码

//无型参无返回值

functest(){

print("Helloc Swift ")

}

test()

//无型参有返回值

func getAppID() ->String{

return "weChat"

}

print("App ID =\(getAppID()) ")

//有型参有返回值

func getMaxValue(value1 :Int, value2 :Int) ->Int{

if value1 > value2{

return value1

}

return value2

}

print("max value =\(getMaxValue(511,value2:11))")

//外部参数

func getMinValue(值一value1 :Int,值二value2 :Int) ->Int{

if value1 > value2{

returnvalue2

}

returnvalue1

}

print("min value =\(getMinValue(值一:100,值二:20))")

//可变参数

func getMaxValueFromArray(numbers :Int...) ->Int{

var maxNumber :NSInteger= numbers[0]

for numinnumbers{

if num > maxNumber{

maxNumber = num

}

}

return maxNumber

}

print("max value =\(getMaxValueFromArray(1,2,2,5,48))")

//函数传值

func swapValue(inoutvalue1 :Int,inoutvalue2 :Int){

let tempValue = value1;

value1 = value2;

value2 = tempValue;

}

varvalue1 =15

varvalue2 =20

print("oldValue: value1 =\(value1) value2 =\(value2)")

swapValue(&value1, value2: &value2)

print("newValue: value1 =\(value1) value2 =\(value2)")

相关文章

网友评论

      本文标题:学习Swift (第四天)

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