当前位置:网站首页>Kotlin function

Kotlin function

2022-07-05 12:42:00 Curious rookie

One 、 Function definition


/*
 keyword      Function name            Parameter type     return type 
 ↓        ↓                ↓       ↓      */
fun helloFunction(name: String): String {
    return "Hello $name !"
}/*   ↑
    Inside the curly brackets is : The body of the function 
*/

Abbreviation

fun helloFunction(name: String): String = "Hello $name !"
fun helloFunction(name: String) = "Hello $name !"

Two 、 Function call

helloFunction("Kotlin")
helloFunction(name = "Kotlin")

3、 ... and 、 Function default parameters


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
) {
    //..
}

  Parameters with default values can be omitted


createUser(
    name = "Tom",
    age = 30,
    commentCount = 3285
)

原网站

版权声明
本文为[Curious rookie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051212050287.html