Basics Using the vararg keyword

suggest change

Define the function using the vararg keyword.

fun printNumbers(vararg numbers: Int) {
    for (number in numbers) {
        println(number)
    }
}

Now you can pass as many parameters (of the correct type) into the function as you want.

printNumbers(0, 1)                // Prints "0" "1"
printNumbers(10, 20, 30, 500)     // Prints "10" "20" "30" "500"
Notes: Vararg parameters must be the last parameter in the parameter list.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents