Vararg in Kotlin (Variable Number of Arguments)

1. Why Use vararg?

Sometimes, you don’t know how many arguments a function should take. Instead of defining multiple overloads or passing a list manually, Kotlin provides the vararg keyword to accept a variable number of arguments.

It allows you to pass multiple values of the same type to a single parameter, making your function calls more flexible and concise.


2. How to Use vararg in Functions

You can declare a vararg parameter in a function to accept zero or more values of the specified type.


// Example: Using vararg in Kotlin
fun printNames(vararg names: String) {
    for (name in names) {
        println(name)
    }
}

fun main() {
    printNames("Talha", "Sara", "Ali")
}
    

Output:


Talha
Sara
Ali
    

3. Accessing vararg Values

Internally, Kotlin stores all vararg parameters as an array. This means you can use all array functions and properties on them.


fun sumAll(vararg numbers: Int): Int {
    return numbers.sum() // numbers is an IntArray
}

fun main() {
    println(sumAll(1, 2, 3, 4)) // Output: 10
}
    

4. Passing Arrays to Vararg Parameters

You can also pass an existing array to a vararg function using the spread operator (*).


fun printLanguages(vararg languages: String) {
    languages.forEach { println(it) }
}

fun main() {
    val langs = arrayOf("Kotlin", "Java", "Python")
    printLanguages(*langs) // Spread operator unpacks the array
}
    

Output:


Kotlin
Java
Python
    

5. Combining Vararg with Other Parameters

You can combine vararg with normal parameters, but the vararg must be the last or a second-to-last parameter (if followed by a parameter with a named argument).


fun greetUsers(greeting: String, vararg users: String) {
    for (user in users) println("$greeting, $user!")
}

fun main() {
    greetUsers("Hello", "Talha", "Sara", "Ali")
}
    

Output:


Hello, Talha!
Hello, Sara!
Hello, Ali!
    

If a parameter comes after a vararg, you must use a named argument when calling the function.


fun registerUsers(vararg names: String, admin: String) {
    println("Admin: $admin")
    println("Users: ${names.joinToString()}")
}

fun main() {
    registerUsers("Ahsan", "Zain", admin = "Talha")
}
    

6. When to Use and When Not to Use vararg

✅ Use vararg when:
🚫 Avoid vararg when:

7. Interview Questions


8. Example Program


fun calculateAverage(vararg scores: Int): Double {
    if (scores.isEmpty()) return 0.0
    return scores.average()
}

fun main() {
    println("Average Score: ${calculateAverage(85, 90, 95)}")
    println("Average Score: ${calculateAverage(60, 70)}")
    println("Average Score: ${calculateAverage()}")
}
    

Output:


Average Score: 90.0
Average Score: 65.0
Average Score: 0.0
    

Summary:
The vararg keyword in Kotlin allows functions to accept a flexible number of arguments without creating overloaded versions or passing arrays manually. It improves readability and flexibility while keeping function calls concise and expressive.