October 2018
Intermediate to advanced
370 pages
9h 15m
English
There is a possibility that we will be asked to create a function with an integer vararg along with two integer variables. See the following example:
fun trickyVararg(vararg list: Int, a : Int, b: Int){ var total = 0 for (item in list){ total += item } println("Total $total") println("a = $a , b = $b")} fun main (args: Array<String>) { trickyVararg(1,2,3,4,5) }
The first three values (1,2,3) are for vararg list; parameter a is assigned with value 4 and parameter b is assigned with value 5. However, the compiler will throw the following errors:
Kotlin: No value passed for parameter 'a'Kotlin: No value passed for parameter 'b'
vararg list is declared first in the function signature, and the compiler considers ...
Read now
Unlock full access