What does the `vararg` modifier do in a Kotlin function parameter, and how do you read its values inside the function body?
answer
- vararg = variable number of args of one type
- Inside body it is an Array (IntArray for Int)
- Only one vararg per function
- Zero args = empty array, never null
- Params after vararg must be named
basics
~10 svararg lets a function accept any number of arguments of one type. You call it like f(1, 2, 3) and inside the function you read them as an array you can loop over.
solid answer
~30 sA parameter marked `vararg` accepts a variable number of arguments of the same type. The caller passes zero, one, or many comma-separated values: `printAll("a", "b", "c")`. Inside the function the parameter is exposed as an `Array<T>` (for object types) or the specialized primitive array like `IntArray` for `vararg x: Int`. You iterate it with a `for` loop, read `.size`, index it, etc. A function may declare only one `vararg` parameter. If it is not the last parameter, any parameters after it must be passed using named arguments. Passing zero arguments yields an empty array, not null.
code
kotlin · 8 linesfun sum(vararg nums: Int): Int { // nums is IntArray
var total = 0
for (n in nums) total += n
return total
}
println(sum(1, 2, 3)) // 6
println(sum()) // 0go deeper
Knows vararg accepts many args and is read as an array via a for loop.
Adds the one-vararg rule, primitive-array specialization, and that later params must be named.
Explains empty-array (not null) semantics and JVM varargs mapping driving the Array type.
Frames API design trade-offs: vararg vs List parameter for ergonomics, ABI, and overload resolution.
## What `vararg` is `vararg` (variable arity) is a parameter modifier that lets a single function accept **any number of arguments** of a given type, supplied as a comma-separated list at the call site. ```kotlin fun printAll(vararg items: String) { for (item in items) println(item) println("count = ${items.size}") } printAll("a", "b", "c") // count = 3 printAll() // count = 0 (empty array, NOT null) ``` ## How it is exposed inside the body Inside the function, the `vararg` parameter is an **array**: - For reference types `vararg items: String` → `Array<String>`. - For primitive types it becomes the specialized primitive array to avoid boxing: `vararg x: Int` → `IntArray`, `vararg x: Long` → `LongArray`, `vararg x: Double` → `DoubleArray`, etc. You use normal array operations: `for`, `items.size`, `items[i]`, `items.isEmpty()`, `items.joinToString()`. ## Rules - A function can have **at most one** `vararg` parameter. - The `vararg` parameter is usually last. If it is **not** last, every parameter declared **after** it must be supplied via **named arguments** at the call site (because positional binding would be ambiguous). - Passing no values produces an **empty array**, never `null`. ```kotlin fun tag(vararg classes: String, id: String) { /* ... */ } tag("a", "b", id = "main") // id MUST be named ``` ## Why arrays (not List) `vararg` maps to a JVM varargs array, so the parameter type is `Array`/`IntArray`, not `List`. If you want a list, call `.toList()` or `.asList()` inside.
- What type is `vararg x: Int` inside the function?`IntArray` — a specialized primitive array, used instead of `Array<Int>` to avoid boxing.
- Can a function have two vararg parameters?No. A function may declare at most one vararg parameter.
Like a checkout lane that accepts a basket with any number of items rather than a fixed number of slots.
saying these in an interview costs you the question
- Saying the parameter is a `List` inside the body (it is an Array)
- Thinking passing no arguments gives `null`
- Claiming you can have multiple vararg params
- Not knowing primitive varargs use IntArray/LongArray etc.