November 2018
Intermediate to advanced
388 pages
9h 5m
English
Kotlin supports a variable number of arguments. vararg is the keyword that is used for a variable number of arguments. Using vararg, we can pass multiple arguments to a function.
Let's see an example of 7a_VarArgs.kts:
fun findMaxNumber(vararg numbers : Int) = numbers.reduce { max , e -> if(max > e) max else e }println(findMaxNumber(1,2,3,4))
The output is as follows:

The findMaxNumber() function takes a variable number of arguments and finds the maximum. We invoked reduce on the numbers and wrote a lambda expression. In Kotlin, a lambda is specified using curly braces.
If we have an array of numbers, we can invoke the ...
Read now
Unlock full access