Variable number of arguments (vararg) in functions

Kotlin supports a variable number of arguments in functions as in the following code:

fun sum(vararg nos: Int): Int {
var result = 0;
for(no in nos) {
result+= no;
}
return result;
}

These can be invoked with a varying number of arguments, such as the following code:

println("Sum of 1, 2, 3 is ${sum(1, 2, 3)}");
println("Sum of 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 is ${sum(0, 9, 8, 7, 6, 5, 4, 3, 2, 1)}");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.80.122