Kotlin return Statement Tutorial

In this section, we will learn what the return statement is and how to use it in Kotlin.

Note: we’re assuming you’re already familiar with the Kotlin functions.

return Statement in Kotlin

In the Kotlin functions section, we mentioned that a function is capable of returning a value when it’s called. This is done using the return statement.

When designing a function, if that function is set to return a value (by specifying the data type of the return value in the signature of the function) then it’s our responsibility to call the return statement within the body of that function to make sure it will return a value at the end.

Alright, let’s get into the details of how to use the return statement now.

Kotlin return Statement Declaration

This is the syntax of using the return statement:

return value

First, we put the keyword return then we use the value we want to return on the right side of this keyword.

Note that the data type of the returned value must be the same as the return data type we specify in the signature of that function.

For example, if we set the function to return a String data type value, then the returned value must be of type String. Otherwise we will get an error.

Note: the moment your program runs the return statement, the work of that function is finished! Basically, it doesn’t matter if you specify other statements in the body of that function and after the return statement, because none of them will be called!

The return statement is a signal to a program that the work of the function is done and the program must return to the caller of the function (where the call occurred). So don’t put other statements after the return statement.

Example: using return statement in Kotlin

fun main(){
    var res = multiply(10,20)
    println(res)
}

fun multiply(num1:Int, num2:Int):Int{
    println("The first value is: ${num1} and the second value is: ${num2}")
    return num1 * num2
}

Output:

The first value is: 10 and the second value is: 20

200

Here you can see the multiply function has set the return type to Int. This means we need to call the return statement within the body of this function and return an integer value as a result.

So what we’ve done here is to first call the multiply function on the right side of the assignment operator in order to pass the return value of this function to the res variable and then within its body, we’ve multiplied the two arguments together and returned the result using the return statement.

Multiple return Statement in Kotlin

In a function, you’re not limited to just one return statement! You can put as many return statement as needed within that function.

This is mainly helpful when branching using conditional statements like if-else or when etc.

For example, you can set a return statement in the body of an if statement as well as the else statement and based on the condition of the if statement, one of the defined return statement will be called.

But remember, the design of the instructions within a function must be set in a way that it is guaranteed at least one of the return statements will be called. Otherwise, you’ll get an error if there was a chance that none of the return statements were called.

Example: using multiple return statement in Kotlin

fun main(){
    var res = divMultOperation("div",600,2)
    println(res)
}

fun divMultOperation(type:String, num1:Int, num2:Int):Int{
    if (type == "div"){
        return num1/num2
    }else if (type == "mult"){
        return num1 * num2
    }else{
        println("The type of operation is not clear!")
        return 0 
    }
}

Output:

300

Look at the body of the divMultOperation; here we have three return statements. Now based on the argument we set for the first parameter of the function, one of these return statements will be triggered and its value will be returned.

Note that we’ve set the return statement in the body of the else statement as well. If we didn’t do that it would’ve been the source of an error. This is because there’s a chance that users might not put the values mult or div as the argument of the function and so the condition of the if and else if might never become true. But the function required a return value no-matter what. For this reason, we’ve set the else statement with a default return statement to make sure the function will return a value even if it gets a wrong type of value.

Kotlin return multiple values

So far we’ve seen that the functions just return a single value. But Kotlin is not limited to a single value, and we’re able to return multiple values if necessary.

For example, we can use arrays to return a range of values instead of just one single value.

All is needed is to set the return value of the target function to an array type and then return the array as a result.

Note: in the Kotlin array section, we’ve explained how to work with arrays in great details.

Example: returning multiple values in Kotlin

fun main(){
    var res = returnMultipleValues()
    for (element in res){
        println(element)
    }
}

fun returnMultipleValues():Array<String>{
    var names = arrayOf("Omid","Jack","Ellen","Richard","Monica","Ami")

    return names 
}

Output:

Omid

Jack

Ellen

Richard

Monica

Ami

Kotlin Function Unit Return Type

If a function does not return a value, we need to set its return data type to Unit.

This value is optional and we can simply remove the data type of function that does not return a value and still the function would work properly.

Example: using Unit as the return type of functions in Kotlin

fun main(){
    sayHello("John Doe")
    sayGoodBye("John Doe")
}

fun sayHello(fullName:String):Unit{
    println("Hello ${fullName}")
}

fun sayGoodBye(fullName:String){
    println("Good bye ${fullName}")
}

Output:

Hello John Doe

Good bye John Doe
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies