October 2019
Intermediate to advanced
434 pages
11h 54m
English
We can define any number of parameters for our functions, but sometimes we may not know how many parameters we need to pass when writing the code. For these situations, it's possible to define a function that takes an unspecified number of parameters of the same type. This is achieved using the vararg modifier. Let's see how this is done.
Adding vararg to a parameter definition will change that parameter from Type to Array<Type>, allowing a caller to pass in multiple argument values. In the following code, we've now updated helloFunctions to take a variable number of the name arguments by adding vararg before the name parameter definition:
fun helloFunctions(greeting: String, vararg names: String) { names.forEach { name ...
Read now
Unlock full access