# Confusion over Named Tuples

**URL:** <https://discourse.julialang.org/t/confusion-over-named-tuples/90558>\
**Category:** New to Julia\
**Created:** [November 20, 2022, 9:44pm UTC](https://discourse.julialang.org/t/confusion-over-named-tuples/90558 "2022-11-20T21:44:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)\
**Post date:** [November 20, 2022, 9:44pm UTC](https://discourse.julialang.org/t/confusion-over-named-tuples/90558/1 "2022-11-20T21:44:49Z")

</div>

I have a function which takes as arguments named tuples. But I’m struggling to get it work as I would hope. As a minimal working example consider.

```
function test(x; a=1.0, b=1.0, c=1.0)
    return x + a + 5*b + 10*c
end 

```

If I run `test(1.0)` I get 17.0 as expected. If I run `test(1.0, b=10.0, c=10.0)` I get 151.0 as expected. But what I would really like is to define a named tuple as a subset of a,b,c and produce output as in the previous examples.

If I create `named_tuple = (b=10.0, c=10.0)` and run `test(1.0, named_tuple...)` I get back 17.0 which is just `test` evaluated at the default values.

In my actual problem I have a function that takes in many named arguments with default values but with the flexibility to specify different values should I wish. I’m creating a subset of named arguments by iterating through the names and values of a data frame and trying to evaluate my function.

Any help would be very much appreciated.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [November 20, 2022, 9:53pm UTC](https://discourse.julialang.org/t/confusion-over-named-tuples/90558/2 "2022-11-20T21:53:07Z")

</div>

I think you just need to use a `;` instead of `,`.

```julia
julia> f(x; a=1, b=2) = x+a+b
f (generic function with 1 method)

julia> nt = (a=2, b=3)
(a = 2, b = 3)

julia> f(1; nt...)
6

```

`;` denotes that you want to splat “named” arguments. It works in many places:

Merge/update named tuples:

```julia
julia> nt1 = (a=1,b=2)
(a = 1, b = 2)

julia> nt2 = (a=3,c=4)
(a = 3, c = 4)

julia> (;nt1..., nt2..., d=5)
(a = 3, b = 2, c = 4, d = 5)

```

Extract only a few components of an object (works for any structs, not just named tuples):

```julia
julia> (;a, b) = (a=1,b=2,c=3)
(a = 1, b = 2, c = 3)

julia> a
1

julia> b
2

```

Something that might help with remembering this: `;` is the “proper” way to create a named tuple – parenthesis without a `;` is just a syntactic sugar:

```julia
julia> (;a=1,b=2) == (a=1,b=2)
true

```

Edit: you probably also have multiple methods defined for your `test` function. `test(1.0, named_tuple...)` should simply raise a `MethodError` if you have defined only the method you have shared.

---

<div class="post-metadata">

**Author:** ![Mark\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_williams/32/7668_2.png) [@Mark\_Williams](https://discourse.julialang.org/u/Mark_Williams)\
**Post date:** [November 23, 2022, 12:10pm UTC](https://discourse.julialang.org/t/confusion-over-named-tuples/90558/3 "2022-11-23T12:10:50Z")

</div>

Thanks, that’s exactly what I was looking for!
