A function can have zero or more parameters. Our function, basicFunction
, takes two parameters, as shown in the following code:
fun basicFunction(name: String, size: Int) { }
Each parameter is defined as parameterName: ParameterType
, in our example, name: String
and size: Int
. Nothing new here.
It gets interesting when parameters have two types that we have already covered—vararg
and lambdas:
fun aVarargFun(vararg names: String) { names.forEach(::println) } fun main(args: Array<String>) { aVarargFun() aVarargFun("Angela", "Brenda", "Caroline") }
A function with a parameter marked with the modifier, vararg
can be called with zero or more values:
fun multipleVarargs(vararg names: String, vararg sizes: Int) { // Compilation error, "Multiple vararg-parameters are prohibited" }
A function can't have multiple vararg
parameters, not even with different types.