August 2018
Beginner
178 pages
4h 56m
English
Variable function arguments are a feature that allows you to pass multiple argument values to a single function argument. This feature is also available in Java, and a function that accepts variable function arguments has three dots after the parameter type, like this:
public int sumNumbers(int... nums)
In Kotlin, you can have the same functionality if you apply the vararg keyword before a parameter name:
fun sumNumbers(vararg num: Int): Int { var result = 0 for (i in num) { result += i } return result}
Now, we can call this function and supply an arbitrary number of arguments to it:
val result = sumNumbers(1, 2, 3, 4, 5)
When calling a function with variable arguments, the compiler packs them into an array. What ...
Read now
Unlock full access