Open In App

Passing Variable Arguments to a Function in Kotlin

Improve
Improve
Like Article
Like
Save
Share
Report

There are a lot of scenarios in which we need to pass variable arguments to a function. In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body. In this article, we will go through all the ways of doing that. We will look at a few examples to demonstrate how to use this feature of Kotlin.

Example

Let’s go through the following steps, where we demonstrate how to pass a variable number of arguments to a function. Using vararg, we can pass comma-separated arguments to a function, where we have defined the single argument to a method as vararg, as in the following example:

Kotlin




fun main (args: Array<String>) {
  someMethod ( "as", "you", "know", "this", "works")
}
 
fun someMethod (vararg a: String) {
  for (a_ in a) {
    println(a_)
  }
}


Also, if you already have an array of values, you can directly pass it using the * spread operator:

Kotlin




fun main(args: Array<String>) {
   val list = arrayOf ("as", "you", "know", "this", "works")
   someMethod (*list)
}
 
fun someMethod (vararg a: String) {
  for (a_in a) {
    println(a_)
  }
}


So basically, vararg tells the compiler to take the passed arguments and wrap them into an array. The spread operator, on the other hand, simply tells the compiler to unwrap array members and pass them as separate arguments, The spread operator-that is, *-is put just before the name of the array being passed in. However, obviously, one may always need to pass on other arguments, named arguments, and so on. In the following example code, we try to pass another argument other than vararg:

Kotlin




fun main (args: Array<String>) {
  val list = arrayof( "as", "you", "know", "this", "works")
  someMethod ( 3, *list)
}
 
fun someMethod (b: Int, vararg a: String) {
  for (a_ in a) {
    println(a_)
  }
}


In the next example, the first argument is similar to the vararg type, but it works:

Kotlin




fun main (args: Array<String>) {
  someMethod ("3", "as", "you", "know", "this", "works")
}
 
fun someMethod (b: String, vararg a: String) {
  printIn ("b: " + b)
  for (a_ in a) {
       println (a_)
  }
}


Output:

b: 3
as
you
know
this
works

So usually, vararg is the last argument passed, but what if we want to pass other arguments after vararg? We can, but they have to be named. That is why the following code will not compile:

Kotlin




fun main (args: Array<String>) {
  someMethod ("3", "as", "you", "know", "this", "works", "what")
}
 
fun someMethod (b: String, vararg a: String, c: String) {
  printin ("b: " + b)
  for (a_ in a) {
    println(a_)
  }
  println("c: " + c)
}


It does not compile because the last string passed in it is considered part of vararg, and the compiler throws an error because we did not pass the value of c. To do it correctly, we need to pass c as a named argument, just as shown here: 

Kotlin




fun main (args: Array<String>) {
  someMethod ("3", "as", "you", "know", "this", "works", c = "what")
}
 
fun someMethod (b: String, vararg a: String, c: String) {
  printin ("b: " + b)
  for (a_ in a){
    println(a_)
  }
  println ("c: " + c)
}


Output:

b: 3
as
you
know
this
works
c:  what

The vararg modifier tells the compiler to take all comma-separated arguments and wrap them into an array, while *-that is the spread operator-unwraps elements of the array and passes them as arguments.

What if we want the first argument to have a default value, like in this example:

Kotlin




fun main (args: Array<String>) {
  someMethod ("3", "as", "you", "know", "this", "works")
}
 
fun someMethod (b: String = "x", vararg a: String) {
  printin ("b: " + b)
  for (a_ in a){
    println (a_)
  }
}


We want all arguments to be considered as part of vararg, but the compiler reads the first argument as b. In this case, naming the passed arguments can solve the problem:

Kotlin




fun main (args: Array<String>) {
  someMethod (a = *arrayOf ("3", "as", "you", "know", "this", "works"))
}
 
fun someMethod (b: String = "x", vararg a: String) {
  println ("b: " + b)
  for (a_ in a){
    println (a_)
  }
}


In the preceding code, the compiler understands that the value of b is not passed, and it takes the default value. Similarly, if you want to have two vararg in your function, you will need to pass named arguments.



Last Updated : 27 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads