Reputation: 14521
I am trying to write a function that should be allowed to take in a variable number of arguments. However, it's not too clear how I can do that in Julia.
Upvotes: 4
Views: 6038
Reputation: 14521
In Julia, as with many other languages, there exists the ability to write Vararg Functions. These functions allow for a variable number of arguments to be passed in. Here a quick reference to the Julia docs on this idea and an example:
julia> varargs(a,b,c...) = (a,b,c)
varargs (generic function with 1 method)
julia> varargs(5, 10)
(5, 10, ())
julia> varargs(3,4,5)
(3, 4, (5,))
julia> varargs(10, 20, 30, 40, 50, 60, 70, 80)
(10, 20, (30, 40, 50, 60, 70, 80))
julia> d = (2,3,4,5,6,7,8,9)
(2, 3, 4, 5, 6, 7, 8, 9)
julia> varargs(1,2,d)
(1, 2, ((2, 3, 4, 5, 6, 7, 8, 9),))
To reiterate, the magic is happening here when we define the varargs
function and write c...
. This notation enables the whole concept of variable-sized arguments.
Upvotes: 5