kotlin function return type code example

Example 1: kotlin method return

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

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

Example 2: function kotlin

// Declare a function in Kotlin
fun happyBirthday(name: String, age: Int): String {
    return "Happy ${age}th birthday, $name!"
}
// Call function
val greeting = happyBirthday("Anne", 32)

Example 3: return type in kotlin function

//Function having two Int parameters with Int return type
fun sum(a: Int, b: Int): Int {
    return a + b
}

Example 4: kotlin function return

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

Tags:

Java Example