object dt05_ForFouncation {
def main(args: Array[String]): Unit = {
<pre name="code" class="objc">// 1.
for (i <- 1 to 2; j <- 1 to 2) println(100 * i + j + "")
// 打印结果
//101
//102
//201
//202
// 2.添加条件表达式
for (i <- 1 to 2; j <- 1 to 2 if i != j) println(100 * i + j + "")
// 打印结果
//102
//201
//3. 函数是有返回值的
def addA(x: Int) = x + 100
val add = (x: Int) => x + 200
println("The result from a founcation is :" + addA(2))
println("The result from a val is :" + add(2))
// The result from a founcation is :102
// The result from a val is :202
//4. 递归
def fac(n: Int): Int = if (n <= 0) 1 else n * fac(n - 1)
println("The result from a fac is :" + fac(6))
//The result from a fac is :720
// 5.传递多个不同的参数
def combine(content: String, left: String = "[", right: String = "]") = left + content + right
println("The result from a combine is :" + combine("I love spark", ">"))
// The result from a combine is :>I love spark]
// 6.
def connected(args: Int*) = { // 不定参数的使用类似于java中的
var result = 0
for (arg <- args) result += arg
result
}
println("The result from a connected is :" + connected(1, 2, 3, 5, 7))
//The result from a connected is :18