# Function arguments

**URL:** <https://discourse.julialang.org/t/function-arguments/61717>\
**Category:** General Usage\
**Tags:** tuple, function, namedtuple, named-tuple\
**Created:** [May 24, 2021, 11:36am UTC](https://discourse.julialang.org/t/function-arguments/61717 "2021-05-24T11:36:31Z")\
**Posts on this page:** 18\
**Page:** 2

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [December 16, 2024, 4:23pm UTC](https://discourse.julialang.org/t/function-arguments/61717/21 "2024-12-16T16:23:18Z")

</div>

I think that default value syntax ambiguous so not allowed. You should do `(;a, b) = (;a=1, b=2)` instead.

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 16, 2024, 4:29pm UTC](https://discourse.julialang.org/t/function-arguments/61717/22 "2024-12-16T16:29:35Z")

</div>

Can you show this in a function definition to get `a + b` like in the example above from 3 years ago?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 16, 2024, 4:44pm UTC](https://discourse.julialang.org/t/function-arguments/61717/23 "2024-12-16T16:44:18Z")

</div>

This syntax you are asking about always threw an error. @Lincoln_Hannah’s message above was merely a feature request that — as of now — never got implemented.

> [@bertlhf](#):
>
> Can you show this in a function definition to get `a + b` like in the example above from 3 years ago?

Just see

> [@Lincoln\_Hannah](#):
>
> f((; A, B)) = A + B

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 16, 2024, 5:09pm UTC](https://discourse.julialang.org/t/function-arguments/61717/24 "2024-12-16T17:09:03Z")

</div>

Thanks for the clarification. I was looking for a feature for adding defaults and stumbled on this old post. I thought (wrongly) it was implemented. This idea of

```julia
f((; A=1, B=2)) = A+B
X = (A=2)
f(X)
Julia> 4

```

is very appealing to me. Is there a different approach to accomplish something like this?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 16, 2024, 9:14pm UTC](https://discourse.julialang.org/t/function-arguments/61717/25 "2024-12-16T21:14:33Z")

</div>

Keyword arguments may have defaults, and one can splat named tuples into keyword arguments:

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

julia> g(n) = f(; n...)
g (generic function with 1 method)

julia> x = (; a = 2)
(a = 2,)

julia> g(x)
4

```

In case it’s not clear:

```julia-repl
julia> (a = 2,) === (; a = 2,) === (; a = 2)
true

```

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 16, 2024, 10:14pm UTC](https://discourse.julialang.org/t/function-arguments/61717/26 "2024-12-16T22:14:55Z")

</div>

@nsajko Thanks for the concise overview

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 17, 2024, 8:53am UTC](https://discourse.julialang.org/t/function-arguments/61717/27 "2024-12-17T08:53:31Z")

</div>

> [@nsajko](#):
>
> `julia> f(; a = 1, b = 2) = a + b`

@nsajko How would the example look like in case of

```julia-auto
f(c; a = 1, b = 2) = a + b + c

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 17, 2024, 9:35am UTC](https://discourse.julialang.org/t/function-arguments/61717/28 "2024-12-17T09:35:37Z")

</div>

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

julia> x = (; a = 2)
(a = 2,)

julia> f(9; x...)
13

```

Not sure if that’s what you’re asking for.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 17, 2024, 10:08am UTC](https://discourse.julialang.org/t/function-arguments/61717/29 "2024-12-17T10:08:29Z")

</div>

A destructured argument can be annotated and given defaults like any other single argument:

```julia
julia> bar((;a, b) = (a=1, b=2)) = a+b
bar (generic function with 2 methods)

julia> bar()
3

```

which makes the unimplemented syntax redundant and deviate from expected rules. That is likely what tomaklutfu meant.  
Edited for proper terminology. `(;a, b)` is the argument assigned to the variables `a` and `b`.

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 17, 2024, 3:12pm UTC](https://discourse.julialang.org/t/function-arguments/61717/30 "2024-12-17T15:12:32Z")

</div>

@nasjko

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

julia> g(n) = f(; n...)
g (generic function with 1 method)

julia> x = (; a = 2)
(a = 2,)

julia> g(x)
4

```

Maybe nice to add is the use of the empty tuple to get the complete default:

```julia
Julia> g(())
3

```

I was wondering about the g function def in case of `f(c ; a = 1, b = 2) = a + b + c`, but the following worked:

```julia
f(c ; a = 1, b = 2) = a + b + c
g(c, n) = f(c; n...)
x = (; a = 2)
julia>g(3, x)
7
Julia>g(3())
6

```

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 17, 2024, 3:20pm UTC](https://discourse.julialang.org/t/function-arguments/61717/31 "2024-12-17T15:20:28Z")

</div>

@Benny Thanks for the annotation idea. Comes very close to the original request from 3 years ago. It is not clear to me, however, how to override a default, say `a=2`. I tried `bar((2,))`, `bar((a=2,))`, ` bar(2,)`, `bar(a=2)`, `bar(;a=2)` but nothing works. What (probably obvious thing) am I missing?

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 17, 2024, 3:22pm UTC](https://discourse.julialang.org/t/function-arguments/61717/32 "2024-12-17T15:22:31Z")

</div>

Typo, should be

```julia
Julia>g(3,())
6

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 17, 2024, 10:22pm UTC](https://discourse.julialang.org/t/function-arguments/61717/33 "2024-12-17T22:22:43Z")

</div>

> [@bertlhf](#):
>
> It is not clear to me, however, how to override a default, say `a=2`

You can’t do it individually for a destructured argument. `bar` only takes 1 argument, and it must have properties `a` and `b` to assign to those variables, e.g. `bar((a=10, b=2))`. If you want to annotate, default, or otherwise handle `a` and `b` separately, then those should be separate arguments e.g. `baz(a=1, b=2) = a-b`

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 18, 2024, 9:26am UTC](https://discourse.julialang.org/t/function-arguments/61717/34 "2024-12-18T09:26:28Z")

</div>

I have a different but related question about function arguments and destructuring, so I ask it here.

Since I’m an iteration newby, I’m wondering what would be a good generic iteration solution, in the scenario below, to make available in case 1 (the minimum case) the variables `a, c, d` and in case 2 (the maximum case) the variables `a, b1, b2, c, d, e1[2], e2[2]`.

```julia

julia> f(a, args...; c, d=4.1, kwargs...) = a, args, c, d, kwargs

f (generic function with 1 method)

# case 1 (minumum case)

julia> a, b, c, d, e = f(1, c=3)

(1, (), 3, 4.1, Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}())

julia> length(b)

0

julia> length(e)

0

julia> println("$a, $c, $d")

1, 3, 4.1

# case 2 (maximum case)

julia> a, b, c, d, e = f(1, 2.1, 2.2, c=3, d=4.2, e1=5.1, e2=5.2)

(1, (2.1, 2.2), 3, 4.2, Base.Pairs(:e1 => 5.1, :e2 => 5.2))

julia> length(b)

2

julia> b1, b2 = b

(2.1, 2.2)

julia> length(e)

2

julia> e1, e2 = e

pairs(::NamedTuple) with 2 entries:

:e1 => 5.1

:e2 => 5.2

julia> println("$a, $b1, $b2, $c, $d, $(e1[2]), $(e2[2])")

1, 2.1, 2.2, 3, 4.2, 5.1, 5.2

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 20, 2024, 4:24am UTC](https://discourse.julialang.org/t/function-arguments/61717/35 "2024-12-20T04:24:43Z")

</div>

Rephrase? I don’t understand what is being asked for.

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 20, 2024, 6:43am UTC](https://discourse.julialang.org/t/function-arguments/61717/36 "2024-12-20T06:43:58Z")

</div>

Sorry, I was not clear. I was wondering about a generic function `items(f())` that does what I did manually above:

```julia
julia> f(a, args...; c, d=4.1, kwargs...) = a, args, c, d, kwargs

julia> items(f(1, c=3))
1, 3, 4.1

julia> items(f(1, 2.1, 2.2, c=3, d=4.2, e1=5.1, e2=5.2))
1, 2.1, 2.2, 3, 4.2, 5.1, 5.2

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [December 20, 2024, 3:54pm UTC](https://discourse.julialang.org/t/function-arguments/61717/37 "2024-12-20T15:54:07Z")

</div>

```julia-repl
julia> f(a, args...; c, d=4.1, kwargs...) = (a, args..., c, d, values(kwargs)...)
f (generic function with 1 method)

julia> f(1, c=3)
(1, 3, 4.1)

julia> f(1, 2.1, 2.2, c=3, d=4.2, e1=5.1, e2=5.2)
(1, 2.1, 2.2, 3, 4.2, 5.1, 5.2)

```

Like that?

---

<div class="post-metadata">

**Author:** ![bertlhf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertlhf/32/38078_2.png) [@bertlhf](https://discourse.julialang.org/u/bertlhf)\
**Post date:** [December 20, 2024, 4:56pm UTC](https://discourse.julialang.org/t/function-arguments/61717/38 "2024-12-20T16:56:59Z")

</div>

Very nice, that’s very much what I was looking for.

But I want to keep the original definition of `f()` as it is, so I defined a `items()` function that produces all the argument items of any call of `f()`:

```julia
julia> f(a, args...; c, d=4.1, kwargs...) = a, args, c, d, kwargs

julia> items((v1, v2, v3, v4, v5)) = v1, v2..., v3, v4, values(v5)...

julia> items(f(1, c=3))
1, 3, 4.1

julia> items(f(1, 2.1, 2.2, c=3, d=4.2, e1=5.1, e2=5.2))
1, 2.1, 2.2, 3, 4.2, 5.1, 5.2

```

@nsajko Thanks very much. So no iteration needed after all.

[Previous page](https://discourse.julialang.org/t/function-arguments/61717.md?page=1)
