一、函数定义
/* 关键字 函数名 参数类型 返回值类型 ↓ ↓ ↓ ↓ */ fun helloFunction(name: String): String { return "Hello $name !" }/* ↑ 花括号内为:函数体 */
简写
fun helloFunction(name: String): String = "Hello $name !"
fun helloFunction(name: String) = "Hello $name !"
二、函数调用
helloFunction("Kotlin")
helloFunction(name = "Kotlin")
三、函数默认参数
fun createUser( name: String, age: Int, gender: Int = 1, friendCount: Int = 0, feedCount: Int = 0, likeCount: Long = 0L, commentCount: Int = 0 ) { //.. }
fun createUser( name: String, age: Int, gender: Int = 1, friendCount: Int = 0, feedCount: Int = 0, likeCount: Long = 0L, commentCount: Int = 0 ) { //.. }
有默认值得参数可以不传
createUser( name = "Tom", age = 30, commentCount = 3285 )