Scala函数和代码块

来源:互联网 发布:java链表声明 编辑:程序博客网 时间:2024/06/05 05:59

一 代码块

{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. }
原创粉丝点击