2.5 Using Multiple Assignments

Passing multiple arguments to methods is commonplace in many programming languages. Returning multiple results from functions, on the other hand, is not that common, though it can be quite convenient. We can return multiple results from functions and assign them to multiple variables in one shot. Simply return an array and use comma-separated variables wrapped in parentheses on the left side of the assignment.

In the next example, we have a function that splits a full name into first and last names. The ​split​ function, as we’d expect, returns an array. We can assign the result of the ​splitName​ function to a pair of variables: ​firstName​ and ​lastName​. Groovy assigns the two values to the two variables, respectively. ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.