This article covers different ways to return multiple values from a function in Kotlin.

1. Using Data class

The idiomatic way of returning multiple values from a function is to define your data class and return its instance from the function. Then you can unpack values using destructuring declaration inside the caller function.

Download Code

2. Using Pair/Triple class

Kotlin has generic Pair and Triple types that can return two or three values from the function. Note that it’s preferred to have your data named properly, and we should use a data class to return more values.

Download Code

3. Returning an array

Another approach is to accumulate your values inside an array and return it from the function. Note that this doesn’t offer type safety for an object array or pass field information to the caller.

Download Code

That’s all about returning multiple values from a function in Kotlin.