ITeye博客

Scala函数和代码块

cakin24 · 2017-08-20 08:01 · Scala · 编程语言 · 代码块 · Markdown

一 代码块

{exp1;exp2}

{

exp1

exp2

}

代码块也是一个表达式,其最终的求值是最后一个表达式的值。

 

二 函数

def functionName(param:ParamType):ReturnType={

//function body:expressions

}

 

三 实例

  1. object func_examples {
  2. def hello(name:String):String={
  3. s"Hello,${name}"
  4. }//> hello: (name: String)String
  5. hello("cakin24")//> res0: String = Hello,cakin24
  6. def hello2(name:String)={
  7. s"Hello,${name}"
  8. }//> hello2: (name: String)String
  9. hello("cakin24")//> res1: String = Hello,cakin24
  10. def Add(x:Int,y:Int)=x+y //> Add: (x: Int, y: Int)Int
  11. Add(1,2)//> res2: Int = 3
  12. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。