What does repeat(n) { } do in Kotlin, and what argument does its lambda receive?
answer
- inline fun repeat(times, action: (Int) -> Unit)
- Index is zero-based: 0..times-1
- Lambda param is 'it' by default
- times <= 0 -> body never runs, no error
- Returns Unit; for side effects only
basics
~10 srepeat(n) runs the block of code n times. Each run, the block gets the current count, starting at 0, so you know which iteration you're on.
solid answer
~30 srepeat is an inline stdlib function: `inline fun repeat(times: Int, action: (Int) -> Unit)`. It executes `action` exactly `times` times, passing the zero-based iteration index (0, 1, ..., times-1) into the lambda. It's a concise replacement for `for (i in 0 until n)` when you need a fixed count and the index. If `times` is 0 or negative, the body never runs. Because it's inline, the lambda is inlined at the call site, so there's no lambda-allocation or call overhead versus a manual loop. The lambda returns Unit, so it's for side effects (printing, mutating, building) rather than producing a value.
code
kotlin · 7 linesrepeat(4) { i ->
println("attempt #${i + 1}")
}
// attempt #1
// attempt #2
// attempt #3
// attempt #4go deeper
Knows repeat(n) runs a block n times and provides a zero-based index.
States the exact signature, that it returns Unit, and the empty-range behaviour for non-positive counts.
Explains inlining (no lambda allocation / call overhead) and when to choose List(n){} over repeat for value production.
Frames repeat as a readability/intent tool, weighs it against for-loops and functional builders, and considers non-local returns enabled by inlining.
## Signature ```kotlin public inline fun repeat(times: Int, action: (Int) -> Unit) { for (index in 0 until times) { action(index) } } ``` `repeat` is a top-level standard-library function in package `kotlin`. It runs `action` `times` times. ## The index parameter The lambda receives a single `Int`: the **zero-based** iteration index. Inside a single-parameter lambda you can use the implicit name `it`, or name it explicitly: ```kotlin repeat(3) { println(it) } // prints 0, 1, 2 repeat(3) { index -> println(index) } // same ``` So `repeat(n)` yields indices `0, 1, ..., n - 1`. ## Edge cases - `repeat(0) { ... }` — body never executes. - `repeat(-5) { ... }` — also never executes, because `0 until times` is an empty range when `times <= 0`. No exception is thrown. ## When to use it Use `repeat` for **fixed-count, side-effecting** loops where you don't need to iterate over a collection: ```kotlin repeat(5) { print("*") } // ***** val sb = StringBuilder() repeat(3) { i -> sb.append("row $i\n") } ``` It reads more clearly than `for (i in 0 until n)` for pure counting. ## Inline performance Because `repeat` is `inline`, the compiler inlines both the function and the lambda body into the call site. There is **no function-call overhead and no lambda object allocated** — it compiles down to essentially the same bytecode as a hand-written `for` loop. That's why it's free to use in hot paths. ## What it is not for The lambda returns `Unit`; `repeat` itself returns `Unit`. It does not collect results. To build a list of computed values, prefer `List(n) { i -> ... }` or `(0 until n).map { ... }` instead.
- What index is passed on the first iteration of repeat(n)?0. The index is zero-based and runs from 0 to n-1.
- What happens with repeat(0) {} or a negative count?The body never runs and no exception is thrown, since the underlying range 0 until times is empty.
Like a metronome ticking a set number of beats, announcing the beat number each tick.
saying these in an interview costs you the question
- Saying the index starts at 1
- Claiming a negative count throws an exception
- Thinking repeat returns the collected results
- Confusing repeat with the String.repeat(n) extension that builds a repeated string
- Believing the lambda receives no argument