定义函数
如果定义的时候没有加(),那么调用的时候也不能加()
函数其实在scala就是个对象,我们可以用个变量来引用他
scala> val f1 = (x:Int, y:Int) => x+y
f1: (Int, Int) => Int = $$Lambda$1068/185209393@4833eff3
scala> f1(1,2)
res1: Int = 3
完整定义的函数
scala> val f3:(Int, Double) => (Double, Int) = {(x:Int, y:Double) => (y,x)}
f3: (Int, Double) => (Double, Int) = $$Lambda$1086/1751080205@3830b06c
scala> f3(1, 1.3)
res2: (Double, Int) = (1.3,1)
变长函数
println(sum(1, 2, 3))
def sum(args: Int*) = {
var result = 0
for (arg <- args) {
result += arg
}
result
}