Reputation: 1079
Julia does not support multiple return, per se. However, Julia performs similar functionality by returning a tuple of values, which can then be assigned to a tuple of variables. For instance:
function mult_return()
(3,2)
end
returns the tuple (3,2)
. We can then assign these two return values to different variables, as follows:
(a,b) = mult_return()
(or a,b = mult_return()
because the parenthesis are not necessary.)
My question is this: Is there a way to ignore one of the return values? For example, in Matlab syntax, a user could write:
[~, b] = mult_return()
so that only the second value is assigned to a variable.
What is the proper way to approach this problem in Julia?
Upvotes: 21
Views: 3811
Reputation: 1058
Rather than assigning the dummy variable _, you can just do
a, = mult_return()
in order to ignore the second return value, and similarly for larger tuples.
Upvotes: 4
Reputation: 57739
I think you can do the same thing that is common in python, namely use underscores for skipped values. Example:
a, _ = mult_return()
It works multiple times as well
_, _ = mult_return()
Upvotes: 19