Variadic Functions in Groovy
Variadic functions can be called with any number of trailing arguments. For example, fmt.Println
is a common variadic function.
Here’s a function that will take an arbitrary number of int
s as arguments.
Within the function, the type of nums
is equivalent to an int[]
. We can use standard array properties and methods on it like length
and iteration.
Variadic functions can be called in the usual way with individual arguments.
If you already have multiple args in an array, apply them to a variadic function using the spread operator (...
) like this.
Running the code will produce the following output:
Another key aspect of functions in Groovy is their ability to form closures, which we’ll look at next.