Passing multiple arguments to a function

In python,

args ={"a":2 ,"c":1,"e":9}

def test(a,c,e):
   print(a,c,e)

test(**args)

My I know the similar way of passing arguments in Julia? Thanks

Here is one way:

test(a,c,e) = println("$a $c $e")

args = (2,1,9)

test(args...)

aslo

test(;a=0,c=0,e=0) = println("$a $c $e")

args = Dict(:a=>2, :c=>1, :e=>9)

test(;args...)
1 Like
julia> args = (a=2, c=1, e=9)
(a = 2, c = 1, e = 9)

julia> test(;a, c, e) = println("$a $c $e")
test (generic function with 1 method)

julia> test(;args...)
2 1 9
1 Like

That rather corresponds to test(*args) in Python.

1 Like

This may lead to unexpected results:


julia> test(a, c, e) = println("$a $c $e")
test (generic function with 1 method)

julia> args1 = (a=2, c=1, e=9)
(a = 2, c = 1, e = 9)

julia> test(args1...)
2 1 9

julia> args2 = (a=2, e=9, c=1)
(a = 2, e = 9, c = 1)

julia> test(args2...)
2 9 1

Edit: @GunnarFarneback is correct. This inconsistency is only because I did not use ; specifying keyword arguments.

Yeah, was way too quick. I’ve edited out that misinformation.