# Function with parameter: \`f(x,p)\` or \`f(x;p)\`

**URL:** <https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745>\
**Category:** Performance\
**Created:** [May 7, 2021, 9:36pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745 "2021-05-07T21:36:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [May 7, 2021, 9:36pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/1 "2021-05-07T21:36:48Z")

</div>

I have been using the call with the key argument to provide parameters `f(x;p=(a=1.1,b=2.2))` taking advantage of broadcasting without `Ref`. However, noticed `f(x,p)` is a common format as well.  
Any thoughts on which way is “better”, more standard, more performant?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [May 7, 2021, 9:59pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/2 "2021-05-07T21:59:10Z")

</div>

I asked a similar question a few weeks ago. This is the thread: [Performance difference between optional args and keyword args](https://discourse.julialang.org/t/performance-difference-between-optional-args-and-keyword-args/59272)

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [May 7, 2021, 10:02pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/3 "2021-05-07T22:02:03Z")

</div>

BTW if you are passing parameters that way, I think you’d be interested in [`ComponentArrays.jl`](https://github.com/jonniedie/ComponentArrays.jl).

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [May 7, 2021, 10:12pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/4 "2021-05-07T22:12:01Z")

</div>

thanks for the reference. The performance aspect is solved!

What is about the “more standard” aspect. Which one would have a better compatibility with other packages?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [May 7, 2021, 10:16pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/5 "2021-05-07T22:16:16Z")

</div>

I think that ML people prefer to pass parameters as arguments, but I wouldn’t call that standard. Myself, I tend to pass them as kwargs.

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [May 7, 2021, 10:16pm UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/6 "2021-05-07T22:16:40Z")

</div>

Really nice, I did not see it, indeed.  
In [AlgebraPDF](https://github.com/mmikhasenko/AlgebraPDF.jl), I work with [NamedTuples](https://github.com/mmikhasenko/AlgebraPDF.jl/blob/master/src/parameters.jl#L47), so everything is immutable. There are pros and cons. Here I also do not really know what is better.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [May 8, 2021, 1:33am UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/7 "2021-05-08T01:33:27Z")

</div>

I think in the announcement post of `ComponentArrays.jl` there is some discussion on performance of several similar structures: [[RFC/ANN] ComponentArrays.jl for building composable models without a modeling language](https://discourse.julialang.org/t/rfc-ann-componentarrays-jl-for-building-composable-models-without-a-modeling-language/38001)

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [May 8, 2021, 6:05am UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/8 "2021-05-08T06:05:18Z")

</div>

One thing is that a `ComponentArray` is a subtype of `AbstractArray`, so you can pass it to optimizers, solvers, etc. For example `Optim`, `NLsolve` …

---

<div class="post-metadata">

**Author:** ![misha\_mikhasenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/misha_mikhasenko/32/5060_2.png) [@misha\_mikhasenko](https://discourse.julialang.org/u/misha_mikhasenko)\
**Post date:** [May 8, 2021, 9:15am UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/9 "2021-05-08T09:15:19Z")

</div>

oh, that is great. I had to reinvent it.

---

<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:** [May 9, 2021, 7:43am UTC](https://discourse.julialang.org/t/function-with-parameter-f-x-p-or-f-x-p/60745/10 "2021-05-09T07:43:39Z")

</div>

> [@misha\_mikhasenko](#):
>
> Any thoughts on which way is “better”, more standard, more performant?

Depends on your use case. If you want to dispatch on `p` in `f`, you have to use a positional argument, otherwise pick what you find most convenient.
