In Kotlin, any function that returns a value, except the value of an expression, requires an explicit return statement to return its value. A code example is as follows:

fun max(a: Int, b: Int): Int {
    if (a > b) {
        return a
    } else {
        return b
    }
}
Copy the code

In Kotlin, if is an expression that returns a value, so the above example could be written like this:

fun max(a: Int, b: Int): Int {
    return if (a > b) {
        a
    } else {
        b
    }
}
Copy the code

In Kotlin, you can use the = symbol directly to return the value of a function. Such a function is called a function literal.

fun sum(a: Int, b: Int) = a + b
fun max(a: Int, b: Int) = if (a > b) a else b
Copy the code

We declare an anonymous function using the fun keyword and implement the function directly using expressions.

val sum = fun(a: Int, b: Int) = a + b
println(sum)
println(sum(1.1))
// Function2<java.lang.Integer, java.lang.Integer, java.lang.Integer>
/ / 2
Copy the code

Note that curly braces ({}) in the body of a function make all the difference. For example:

val sumf = fun(a: Int, b: Int) = { a + b }       // Returns a Lambda expression

println(sumf)
println(sumf(1.1))
println(sumf(1.1() ())2) println (sumf (1.1). The invoke () ()3)// Function2<java.lang.Integer, java.lang.Integer, kotlin.jvm.functions.Function0<? extends java.lang.Integer>>
// Function0<java.lang.Integer>
/ / 2
/ / 2
Copy the code

Use the invoke() function to implement the actual calling function. In Kotlin, the () operator corresponds to an overloaded function of the class, such as invoke(). In other words, PI (2) and PI (3) are equivalent.

We can also use the fun keyword and the function name to declare a function. Here is another example of a function with curly braces {} :

fun sumf(a: Int, b: Int): () - >Int = { a + b }

println(sumf(1.1))
println(sumf(1.1).invoke())

fun maxf(a: Int, b: Int): () - >Int = { if (a > b) a else b }
println(maxf(1.2))
println(maxf(1.2).invoke())
// Function0<java.lang.Integer>
/ / 2
// Function0<java.lang.Integer>
/ / 2
Copy the code

As you can see, sumf() and maxf() return functions of type ()-> Int.

A return statement in Kotlin returns from the nearest or anonymous function, but when a return statement is encountered in a Lambda expression, the nearest outer function is returned directly. For example, the following two functions are different:

fun main(a) {
    val intArray = intArrayOf(1.2.3.4.5)

    intArray.forEach {
        if (it == 3) {
            return
        }
        println(it)
    }
}
/ / 1
/ / 2
Copy the code

It returns when 3 is encountered (similar to the break statement in the body of the loop). Instead, we pass forEach an anonymous function called fun(a: Int). The return from this anonymous function does not break out of the forEach loop, like a continue:

fun main(a) {
    val intArray = intArrayOf(1.2.3.4.5)

    intArray.forEach(fun(a: Int) { // This is an anonymous function
        if (a == 3) return // Returns from the nearest function, the anonymous fun(a: Int) above, but the loop continues
        println(a)
    })
}
/ / 1
/ / 2
/ / 4
/ / 5
Copy the code