码迷,mamicode.com
首页 > 其他好文 > 详细

Kotlin(二) 函数定义

时间:2017-11-17 11:58:56      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:函数定义   表达式   turn   nbsp   int   返回   函数   返回值   参数   

1.不带参数,不返回值的函数

  fun sum(){}

2.带参数,不带返回值的函数

  fun sum(a:Int){}

3.带参数,带返回值的函数

  fun sum(a:Int,b:Int) : Int{ return a+b}

4.将表达式作为函数体、返回值类型自动推断的函数

  fun sum(a:Int,b:Int) = a+b

5.函数返回无意义的值

  fun println(a: Int, b: Int) : Unit{ println("sum of $a and $b is ${a + b}") }

  Unit 返回类型可以省略,比如上述方法可写成:

  fun println(a: Int, b: Int) { println("sum of $a and $b is ${a + b}") }

Kotlin(二) 函数定义

标签:函数定义   表达式   turn   nbsp   int   返回   函数   返回值   参数   

原文地址:http://www.cnblogs.com/done-dyj/p/7850671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!