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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access