Passing Variable Arguments to a Function in Kotlin
Last Updated :
27 Jan, 2023
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.
Similar Reads
How to Specify Default Values in Kotlin Functions? In Kotlin, you can provide default values to parameters in a function definition. If the function is called with arguments passed, those arguments are used as parameters. However, if the function is called without passing argument(s), default arguments are used. But If you come from the Java world,
4 min read
How to Pass a Function as a Parameter to Another in Kotlin? In Kotlin, we have the amazing power to work with higher-order functions. These are special kinds of functions that can take other functions as parameters or even return functions as results. This feature makes our code more flexible, cleaner, and easier to understand. In fact, many useful functions
3 min read
Working with Anonymous Function in Kotlin In Kotlin, we can treat functions as expressions by creating lambdas. A lambda is a function literal, meaning itâs a function that doesnât have a name and is defined directly as an expression. These lambda expressions can also be passed as parameters to other functions. However, one important thing
3 min read
How to Write swap Function in Kotlin using the also Function? Swapping two numbers is one of the most common tasks in programming. Usually, when we swap two values in languages like Java or C++, we either use a third temporary variable or pointers/references to perform the swap. In Java, since we donât have pointers, we almost always rely on a third variable t
3 min read
Suspend Function In Kotlin Coroutines Prerequisite: Kotlin Coroutines on Android The Kotlin team defines coroutines as âlightweight threadsâ. They are sort of tasks that the actual threads can execute. Coroutines were added to Kotlin in version 1.3 and are based on established concepts from other languages. Kotlin coroutines introduce a
4 min read