# How to use \`kwargs\` to avoid passing around keyword arguments?

**URL:** <https://discourse.julialang.org/t/how-to-use-kwargs-to-avoid-passing-around-keyword-arguments/84856>\
**Category:** General Usage\
**Tags:** question, kwargs\
**Created:** [July 27, 2022, 1:40am UTC](https://discourse.julialang.org/t/how-to-use-kwargs-to-avoid-passing-around-keyword-arguments/84856 "2022-07-27T01:40:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cory\_Simon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cory_simon/32/3950_2.png) [@Cory\_Simon](https://discourse.julialang.org/u/Cory_Simon)\
**Post date:** [July 27, 2022, 1:40am UTC](https://discourse.julialang.org/t/how-to-use-kwargs-to-avoid-passing-around-keyword-arguments/84856/1 "2022-07-27T01:40:40Z")

</div>

I found the documentation on `kwargs` to be a little terse… how can I exploit keyword arguments to avoid copying tons of different keyword arguments when passing them function to function?

my example below. seems unnatural to write out so many keyword arguments and keep track of them in two places. or is this what needs done? note `_viz_posterior_fit!` will be called elsewhere. thx for tips/insights. will pass on to my students 🙂

```julia
function _viz_posterior_fit!(ax::Axis, data::DataFrame, bayes_res::NamedTuple;
	                         show_bands::Bool=true, 
						     show_function_samples::Bool=false, 
						     show_unobs_data::Bool=true, 
						     show_model::Bool=true,
						     show_legend::Bool=true)
    # tons of code to modify ax

end

```

```julia
function viz_posterior_fit(data::DataFrame, bayes_res::NamedTuple; 
						   show_bands::Bool=true, 
						   show_function_samples::Bool=false, 
						   show_unobs_data::Bool=true, 
						   show_model::Bool=true,
						   show_legend::Bool=true,
						   savename::Union{Nothing, String}=nothing)
	fig = Figure()
	ax = Axis(fig[1, 1], xlabel="time [min]", ylabel="mass uptake [ng/cm²]")
	_viz_posterior_fit!(ax, data, bayes_res; 
	show_bands=show_bands, show_function_samples=show_function_samples, show_unobs_data=show_unobs_data, show_model=show_model, show_legend=show_legend)
	if ! isnothing(savename)
		save(savename * "_i_obs$(bayes_res.i_obs).pdf", fig)
	end
	return fig
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 27, 2022, 1:49am UTC](https://discourse.julialang.org/t/how-to-use-kwargs-to-avoid-passing-around-keyword-arguments/84856/2 "2022-07-27T01:49:49Z")

</div>

If you are only passing the arguments around to the inner function, you can use something like this:

```julia
julia> _f(x; a = 1, b = 2) = x + a + b # inner function, define all parameters explicitly
_f (generic function with 1 method)

julia> f(x; kargs...) = _f(x; kargs...) # only pass the kargs... over
f (generic function with 1 method)

julia> f(1)
4

julia> f(1; a = 2)
5

```

With that you can add keyword parameters to your inner function without having to modify the outer interface.

The other alternative, which is also common, is to define a structure with all parameters and pass that instead, like:

```julia
julia> Base.@kwdef struct Options
           a::Int = 1
           b::Int = 2
       end
Options

julia> _f(x; opt = Options()) = x + opt.a + opt.b
_f (generic function with 1 method)

julia> f(x; opt=Options()) = _f(x; opt=opt)
f (generic function with 1 method)

julia> f(1)
4

julia> f(1; opt = Options(a=2))
5

```

In some situations this provides a more explicit and clearly maintainable code, I think.

---

<div class="post-metadata">

**Author:** ![Cory\_Simon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cory_simon/32/3950_2.png) [@Cory\_Simon](https://discourse.julialang.org/u/Cory_Simon)\
**Post date:** [July 27, 2022, 2:13am UTC](https://discourse.julialang.org/t/how-to-use-kwargs-to-avoid-passing-around-keyword-arguments/84856/3 "2022-07-27T02:13:32Z")

</div>

😍 thank you! also can mix `kargs` with another keyword argument. awesome.

```julia
_f(x; a = 1, b = 2) = x + a + b
f(x; c=0.0, kargs...) = _f(x; kargs...) + c
f(1.0, c=3.0) # 7.0
f(1.0) # 4.0

```
