您的位置:首页 > 移动开发 > Swift

swift——复合类型——函数——参数 返回类型

2017-05-16 14:01 302 查看

parameter

无parameter

func feed()
{
print("feed nothing")
}

调用:
feed()

单个parameter

func feed(rice: Int)
{
print("feed rice \(rice)")
}

调用:
feed(5)

多个parameter

func feed(rice: Int, meat: Int)
{
print("feed rice \(rice) and meat \(meat)")
}

调用:
feed(5, meat: 8)

argument label

argument label用来在函数调用时描述实参
argument和parameter都译为参数,但实际意义有区别:
parameter:特指形参
argument:特指实参

首个参数

无argument label

func feed(rice: Int)
{
print("feed rice \(rice)")
}

调用:
feed(5)
//feed(rice: 5)

含argument label

func feed(main rice: Int)
{
print("feed rice \(rice)")
}

调用:
feed(main: 5)
//feed(rice: 5)
//feed(5)

非首个参数

implicit argument label

func feed(rice: Int, meat: Int)
{
print("feed rice \(rice) and meat \(meat)")
}

调用:
feed(5, meat: 8)
//feed(5, 8)

explicit argument label

func feed(rice: Int, minor meat: Int)
{
print("feed rice \(rice) and meat \(meat)")
}

调用:
feed(5, minor: 8)
//feed(5, meat: 8) //feed(5, 8)

omit argument label

func feed(rice: Int, _ meat: Int)
{
print("feed rice \(rice) and meat \(meat)")
}

调用:
feed(5, 8)
//feed(5, meat: 8)

总结

argument label作用仅仅是描述实参,因此函数体内不能引用argument label,argument label也无需唯一型,可重复
首个参数无默认argument label,或默认argument label为占位符(_),想使用argument label需显式指定
非首个参数含默认argument label,默认argument label与形参名相同,可显式指定argument label
如果非首个参数argument label想omit,显式指定非首个参数argument label为占位符(_)

默认参数值

func feed(rice: Int, meat: Int = 8)
{
print("feed rice \(rice) and meat \(meat)")
}

调用:
feed(5)
feed(5, meat: 8)

总结:
无默认值参数置于参数列表前,含默认值参数置于参数列表后

可变参数

func feed(foods: Int...)
{
for food in foods
{
print("feed food \(food)")
}
}

调用:
feed(5)
feed(5, 8)
feed(5, 8, 58)

总结:
可变参数用3个句号(...)标志,函数至多允许1个可变参数
可变参数本质为Array,可变参数对应实参构造成Array传递给可变参数,因此可变参数对应实参必须为同一类型,且与可变参数类型一致
可变参数不允许含默认值

inout参数

func swapInt(inout a: Int, inout _ b: Int, _ flag: Bool)
{
if(flag == true)
{
let t = a
a = b
b = t
}
}

调用:
//swapInt(&5, &8, true)

let ca = 5
let cb = 8
//swapInt(&ca, &cb, true)

var va = 5
var vb = 8
swapInt(&va, &vb, true)
print("va = \(va), vb = \(vb)")

总结:
inout参数用inout标志,inout标志放在形参名前,或者放在argument label前(如果含argument label)
不允许把immutable值传给inout参数,immutable值包括字面值常量和constant(let定义),只允许把mutable值传给inout参数(var定义),实参传递时用&前缀修饰
非inout参数(默认)都是constant(let定义),因此不允许在函数体内改变非inout参数值
inout参数不允许含默认值

返回值

无返回值

func feed(rice: Int) -> Void
{
print("feed rice \(rice)")
}

注:无返回值本质为空tuple,可用()或Void表示,可omit不写,建议omit不写(节省代码敲入量)

单个返回值

func feed(rice: Int) -> Int
{
print("feed rice \(rice)")
return rice
}

注:单个返回值本质为只含单个成员tuple,可用(Int)表示,可omit括号,建议omit括号(节省代码敲入量)

多个返回值

func feed(rice: Int, meat: Int) -> (Int, Int)
{
print("feed rice \(rice) and meat \(meat)")
return (rice, meat)
}

注:多个返回值本质为含多个成员tuple,不可omit括号,为增加代码可读性,可对tuple成员自定义名字,如(rice: Int, meat: Int)

总结

函数返回类型本质为tuple
无返回值:空tuple,可用()或Void表示,可omit不写,建议omit不写(节省代码敲入量)
单个返回值:只含单个成员tuple,可用(Int)表示,可omit括号,建议omit括号(节省代码敲入量)
多个返回值:含多个成员tuple,可用(Int, Int)表示,不可omit括号,为增加代码可读性,可对tuple成员自定义名字,如(rice: Int, meat: Int)

optional

参数optional

func feed(rice: Int?, meat: Int?)
{
if let food = rice
{
print("feed rice \(food)")
}

if let food = meat
{
print("feed meat \(food)")
}
}

返回值optional

tuple成员optional

func feed(rice: Int, meat: Int) -> (Int?, Int?)
{
let food1: Int? = rice >= 0 ? rice : nil
let food2: Int? = meat >= 0 ? meat : nil

return (food1, food2)
}

tuple optional

func feed(rice: Int, meat: Int) -> (Int, Int)?
{
let food: (Int, Int)? = rice >= 0 && meat >= 0 ? (rice, meat) : nil
return food
}

函数识别

形参名不同

func feed(rice: Int)
{
}

func feed(meat: Int)
{
}

注:编译error,重复声明,feed(5),编译器无法确定调用哪个函数

argument label不同

func feed(main rice: Int)
{
}

func feed(minor rice: Int)
{
}

注:编译正确,feed(main: 5)或feed(minor: 5),编译器通过argument label能确定调用哪个函数

返回类型不同

func feed(rice: Int)
{
}

func feed(rice: Int) -> Int
{
return rice;
}

注:编译正确,let food = feed(5),二义性error,let food: Int = feed(5),编译正确,无二义性错误

总结

函数名 + argument label + 返回类型唯一确定函数
不要仅通过返回类型不同来区分函数,易产生二义性error
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐