Scala 函数

环境: CentOS 6.3

 

看例子,说实话。

 

入门函数:

scala> def max(x: Int,y: Int): Int = {
     | if (x > y) x           //竖线| 是换行是自动显示的,不是代码本身
     | else y
     | }
max: (x: Int, y: Int)Int //上面代码敲完后,这行就会自动出来

scala> max(3,6)
res3: Int = 6

scala> max(3,1)
res4: Int = 3

 

解析:def                                        max           (x: Int,                                    y: Int):                               Int  

            函数定义标识符                函数名字      参数名及类型                     参数名及类型                 函数值返回类型

   

 

返回值为空函数:

scala> def greet() = print("this is no result type function")
greet: ()Unit

scala> greet
this is no result type function

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章