Open In App

Multiple Return Values in Kotlin

Last Updated : 22 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, App code, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code. In this article, we are going to discuss how to return multiple values, but before going ahead you should know some basic things as When we declare a function it performs a specific task and finally it may return a value that’s the meaning of returning values. but generally, the function returns not more than one value, so we hope you know this kind of basic stuff, but in case you don’t then please refer to this article Kotlin functions.

Example

Let’s say we wanted to calculate both the positive and negative square roots of an integer. We could approach this problem by writing two different functions: 

Kotlin




fun positiveRoot(k: Int): Double {
  require(k >= 0)
  return Math.sqrt(k.toDouble())
}
  
fun negativeRoot(k: Int): Double {
  require(k >= 0)
  return -Math.sqrt(k.toDouble())
}


Another approach might be to return an array so we only have to invoke one function:

Kotlin




fun roots(k: Int): Array<Double> {
  require(k >= 0)
  val root = Math.sqrt(k.toDouble())
  return arrayOf(root, -root)
 }


However, we do not know from the return type whether the positive root or negative root is at position 0. We will have to hope the documentation is correct; if not, inspect the source code. We could improve this further by using a class with two properties that wrap the return values:

Kotlin




class Roots(pos: Double, neg: Double)
fun roots2(k: Int): Roots {
  require(k >= 0)
  val root = Math.sqrt(k.toDouble())
  return Roots(root, -root)
}


This has the advantage of having named fields so we could be sure which is the positive root and which is the negative root. An alternative to a custom class is using the Kotlin standard library Pair type. This type simply wraps two values, which are accessed via the first and second fields:

Kotlin




fun roots3(k: Int): Pair<Double, Double> {
  require(k >= 0)
  val root = Math.sqrt(k.toDouble())
  return Pair(root, -root)
}


This is most often used when it is clear what each value means. For example, a function that returned a currency code and an amount would not necessarily need to have a custom class, as it would be obvious which was which. Furthermore, if the function were a local function, you might feel that creating a custom class would be an unnecessary boilerplate for something that will not be visible outside of the member function. As always, the most appropriate choice will depend on the situation.

Note: There exists a three-value version of the pair, which is appropriately named triple.

We can improve this further by using destructuring declarations on the caller site. Destructuring declarations allow the values to be extracted into separate variables automatically:

val (pos, neg) = roots3 (16)

Notice that the variables are contained in a parenthesis block; The first value will be assigned to the positive root, and the second value will be assigned to the negative root. This syntactic sugar works with any object that implements a special component interface. The built-in Pair type, and all data classes, automatically implement this interface.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads