# Pass function arguments in tuple, how to manage keyword arguments

**URL:** <https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034>\
**Category:** New to Julia\
**Created:** [February 21, 2019, 4:23am UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034 "2019-02-21T04:23:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [February 21, 2019, 4:23am UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034/1 "2019-02-21T04:23:08Z")

</div>

I have a function with some optional keyword arguments.

```julia
function f1(a::Int, b::Float64; c::Int=42, d::Int=43)

```

Another top level function may sometimes call `f1`, but I don’t want to bother the user with specifying `f1`’s arguments if they’re not necessary. And when the arguments are required, I want to make it as easy to manage as possible.  
This is my partial solution so far

```julia
function f_top(call_f1::Bool; f1_params::Union{Tuple,Nothing}=nothing)
    if call_f1
        isnothing(f1_params) && throw(ArgumentError)
        f1(f1_params...)
    end
end

```

So this works

```julia
f1_params = (1, 2.0)
f_top(true, f1_params=f1_params)

```

But what if I want to specify `c` and/or `d`? Doesn’t seem possible using this approach. Is there a more idiomatic solution to this type of problem?

(This is obviously a MWE and in my real problem `f1` has around 20 parameters. It’s actually a constructor for a `struct`).

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [February 21, 2019, 4:33am UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034/2 "2019-02-21T04:33:03Z")

</div>

How about

```julia
function f_top(call_f1::Bool; f1_params::Union{Tuple,Nothing}=nothing, f2_kwargs...)
    if call_f1
        isnothing(f1_params) && throw(ArgumentError)
        f1(f1_params...; f2_kwargs...)
    end
end

```

with `f1(a::Int, b::Float64; c::Int=42, d::Int=43) = a+b+c+d`

```julia
julia> f_top(true, f1_params=(1,2.0), c=3)
49.0

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 21, 2019, 10:11am UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034/3 "2019-02-21T10:11:30Z")

</div>

> [@Adriel](#):
>
> Doesn’t seem possible using this approach. Is there a more idiomatic solution to this type of problem?

For many keyword arguments that are passed around/downstream this way, it is common to wrap them in a `struct` defined for the purpose. `Base.@kwdef` provides a nice interface for construction.

---

<div class="post-metadata">

**Author:** ![Adriel](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@Adriel](https://discourse.julialang.org/u/Adriel)\
**Post date:** [February 21, 2019, 9:45pm UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034/4 "2019-02-21T21:45:31Z")

</div>

Beautiful, `Base.@kwdef` is perfect! Thanks.

My focus here is to develop the nicest possible interface, so that my reluctant colleagues have no excuses not to start using julia.

---

<div class="post-metadata">

**Author:** ![pablosaa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosaa/32/20168_2.png) [@pablosaa](https://discourse.julialang.org/u/pablosaa)\
**Post date:** [March 3, 2022, 9:16pm UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034/5 "2022-03-03T21:16:31Z")

</div>

Can you provide an example how it works using `Base.@kwdef` ?

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 3, 2022, 9:31pm UTC](https://discourse.julialang.org/t/pass-function-arguments-in-tuple-how-to-manage-keyword-arguments/21034/6 "2022-03-03T21:31:55Z")

</div>

It could look like this:

```julia
julia> Base.@kwdef struct Params
           a :: Int # Mandatory arguments
           b :: Float64
           c :: Int = 42 # Optional arguments with a default value
           d :: Int = 43
       end
Params

julia> function f1(p::Params)
           # get the arguments values from the fields of p
           p.a * p.b + p.c - p.d
       end
f1 (generic function with 1 method)

# instantiate a set of parameters
# (c will get its default value)
julia> p = Params(a=1, b=3.14, d=12)
Params(1, 3.14, 42, 12)

# call f1 with these parameters
julia> f1(p)
33.14

```

Beware: IIRC `Base.@kwdef` is not part of Julia’s public API
