Although Kotlin doesn't provide a multiple return feature, thanks to data classes and destructuring declarations, it is quite convenient to write functions that return a number of values of different types. In this recipe, we are going to implement a function returning the result of dividing two numbers. The result is going to contain the quotient and remainder values.
Returning multiple data
How to do it...
- Let's start with declaring a data class for the return type:
data class DivisionResult(val quotient: Int, val remainder: Int)
- Let's implement the divide() function:
fun divide(dividend: Int, divisor: Int): DivisionResult {
val quotient = dividend.div(divisor)
val remainder = dividend.rem(divisor...