Kotlin's vararg keyword allows us to create functions that accept a variable number of arguments. This can be useful when we want to write a function that can take any number of arguments, without having to explicitly specify the number of arguments in the function definition.
In this post, we'll learn how to create vararg functions in Kotlin, and how to use them in our code. We'll also look at some examples of how vararg functions can be used to simplify our code.
To create a vararg function in Kotlin, we use the vararg keyword before the parameter name in the function definition. For example, let's say we want to create a function that takes a variable number of strings and prints them out. We could do this with the following function definition:
fun printStrings(vararg strings: String) {
for (string in strings) {
println(string)
}
}
Notice that we've placed the vararg keyword before the strings parameter. This tells Kotlin that the function can take any number of string arguments.
We can call this function with any number of string arguments, and they will all be printed out:
printStrings("Hello", "World") // Prints "Hello" and "World"
printStrings("Kotlin", "is", "awesome") // Prints "Kotlin", "is", and "awesome"
We can also call the function with no arguments, in which case nothing will be printed:
printStrings() // Prints nothing
When we create a vararg function, we can access the vararg parameters in the function body just like any other parameter. In the printStrings() function above, we accessed the strings parameter in a for loop to print out all of the strings.
We can also access the vararg parameters outside of the function body. To do this, we use the * operator before the parameter name when we call the function. For example, let's say we have a vararg function that takes a variable number of integers and prints them out:
fun printInts(vararg ints: Int) {
for (int in ints) {
println(int)
}
}
We can call this function with any number of integer arguments, and they will all be printed out:
printInts(1, 2, 3) // Prints 1, 2, and 3
printInts(4, 5, 6, 7, 8) // Prints 4, 5, 6, 7, and 8
We can also call the function with no arguments, in which case nothing will be printed:
printInts() // Prints nothing
Now let's say we have an array of integers and we want to print them all out using the printInts() function. We can do this by using the * operator to "unpack" the array into individual arguments:
val intArray = intArrayOf(1, 2, 3)
printInts(*intArray) // Prints 1, 2, and 3
We can also create vararg functions that have both vararg and non-vararg parameters. For example, let's say we want to create a function that takes a variable number of strings and an integer, and prints out the strings a number of times equal to the integer:
fun printStringsNTimes(n: Int, vararg strings: String) {
for (i in 1..n) {
for (string in strings) {
println(string)
}
}
}
In this function, we have a non-vararg parameter (n) and a vararg parameter (strings). We can call this function with any number of string arguments, and they will all be printed out n times:
printStringsNTimes(3, "Hello", "World") // Prints "Hello" and "World" three times
printStringsNTimes(5, "Kotlin", "is", "awesome") // Prints "Kotlin", "is", and "awesome" five times
We can also call the function with no string arguments, in which case nothing will be printed:
printStringsNTimes(3) // Prints nothing
In this post, we learned how to create vararg functions in Kotlin, and how to use them in our code. We also looked at some examples of how vararg functions can be used to simplify our code.
If you want to learn more about Kotlin, check out the Kotlin documentation.