Kotlin return Statement

We have often encountered scenarios where we want to use a function to return a value, like say a function to return a Boolean if passed String contains special characters, this is what exactly we will try to understand in this tutorial. Kotlin is a simple way of doing it by using return statement.

Kotlin return Statement

Return Statement allows us to return a value from function or anonymous function or a simple inline function and returning from a lambda expression. We can use return in cases when we want flow to be returned to the enclosing loop with a particular value, let’s have a look at below example which returns the message when called inside the function.

fun main(args: Array<String>) {
    val s = message()
    println("Message = $s")
}

fun message():String{
    return "Hello Kotlin! This is a returned message."
}

Output

Message = Hello Kotlin! This is a returned message.

The code above is quite relevant and we have often done it  in most of the programming languages, since functions were designated to return some value. Lets modify the above code to have a return in between the function.

fun main(args: Array<String>) {
    val s = message(1)
    println("Message = $s")
}
fun message(num: Int):String{
    if(num == 1) return "Hello Kotlin ! This is a returned message from if."
    else return "Hello Kotlin ! This is a returned message from else"
}

Output

Message = Hello Kotlin ! This is a returned message from if.

The output in case we pass any other number in function would be:

Message = Hello Kotlin ! This is a returned message from else

In the scenario above the flow was returned to nearest enclosing function but what if there are cases when we don’t want flow to return to enclosing function, in those cases we can use the Labelled Return.

Labelled return

Labelled return are bit an advanced and can be used in cases when we want flow to be redirected to a specified line in the program. Let’s have a look at different ways in which we can use labelled return statements. The below examples demonstrates the same for us.

Returning Flow to the Specified Line

As simple as it sound, by simply calling the return@label we can divert the flow to the label with specified line. We are taking a case to print sum of 4 numbers in an array whose value is not in between 5 to 20. Let’s see how we can solve this problem using labelled return.

fun main(args: Array<String>) {
   calculateSum(listOf(15,25,3,10))
}
 fun calculateSum(intArray: List<Int>) {
    var sum=0
    intArray.forEach loop@ {
        if (it >5 && it<20) return@loop 
        sum = sum+it
    }
    println("Sum as per condition is : $sum")
 }

Output

Sum as per condition is : 28

So, the statement: return@loop will make the program redirect the control form the if condition to the label loop. The above program is used to check if each time the value of list inside the loop is between permissible values, it calculates the result else redirects the flow to the loop@.

The application of return is for many scenarios viz. Functions of all types (anonymous function, a simple inline function) and lambda expression.

The syntax of the return will remain the same. We will use and refer it when we cover the concept of functions in details.

Leave a Comment

Your email address will not be published. Required fields are marked *