Kotlin vararg : Function, Spread Operator * Example

Home » Kotlin » Kotlin vararg : Function, Spread Operator * Example

One of the language’s powerful features is the ability to handle variable-length arguments using the vararg keyword. Let’s explore the Kotlin vararg feature and understand how it simplifies function parameter handling.

Kotlin vararg : Function, Spread Operator  Example

What is Kotlin vararg?

The vararg keyword in Kotlin allows you to pass a variable number of arguments of the same type to a function. It essentially lets you treat multiple values as an array, providing a convenient way to work with an arbitrary number of arguments.

Varargs enable you to separate arguments with commas, allowing you to pass an arbitrary number of arguments.

Function with vararg parameter

To declare a vararg parameter in Kotlin, you simply prefix the parameter type with the vararg keyword. Here’s an example

fun main()
{
    val os = androidVersion("Cupcake","Froyo","Oreo","Lollipop")

}

fun androidVersion(vararg names: String)
{
    for (name in names)
    {
        println("version:$name")
    }
}
version:Cupcake
version:Froyo
version:Oreo
version:Lollipop

Inside the main function, a variable os is declared and assigned the result of calling the androidVersion function with four arguments: “Cupcake”, “Froyo”, “Oreo”, and “Lollipop”.

The androidVersion function is defined with a vararg parameter names. It means that this function can accept any number of arguments of type String.

Inside the androidVersion function, there is a loop that iterates over each name in the names array.

For each iteration, the function prints out the message “version:” followed by the current name.

The main purpose of this code is to demonstrate how to pass multiple arguments to a function using varargs and iterate over them.

Let’s add a prefix

fun prefixAndroidVersion(vararg names:String, prefix: String )
{
    for (name in names) {
        println(prefix + name)
    }
}
fun main()
{
 val osPrefix = prefixAndroidVersion("Kitkat","Jellybean","Eclair", prefix = "Hello!")
}

Output

Hello!Kitkat
Hello!Jellybean
Hello!Eclair

In Java, the absence of a value-passing mechanism restricts the addition of another parameter of the same type after a vararg. However, named parameters overcome this limitation, enabling you to include an extra parameter of the same type while retaining the ability to pass values.

With the aid of named parameters, you have the flexibility to assign a distinct value to the prefix parameter, independent of the vararg.

Kotlin Special Spread Operator *

During runtime, a vararg behaves like an array. To pass it as an argument to a vararg parameter, you can utilize the special spread operator “*”, which allows you to transmit the vararg’s contents.

fun listOs(vararg names: String)
{
    androidVersion(*names)
}
fun androidVersion(vararg names: String)
{
    for (name in names)
    {
        println("version:$name")
    }
}
fun main()
{
 listOs("Cupcake","Jellybean")
}

Output

version:Cupcake
version:Jellybean

The Kotlin vararg feature simplifies function parameter handling by providing a flexible and convenient way to work with variable-length arguments.

You may also like...