# Proper way to define functions with additional parameters

**URL:** <https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656>\
**Category:** New to Julia\
**Created:** [February 14, 2023, 11:41pm UTC](https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656 "2023-02-14T23:41:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinfreedom](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jinfreedom](https://discourse.julialang.org/u/jinfreedom)\
**Post date:** [February 14, 2023, 11:41pm UTC](https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656/1 "2023-02-14T23:41:18Z")

</div>

Hi, I have the need to define functions that takes arguments and parameters, where the latter often times don’t change for a given scenario (e.g., a physical model with many parameters). I’m doing it in the following way

```julia
struct myParamFun
       fun::Function
       param
end
(pf::myParamFun)(x...)=pf.fun(pf.param,x...)

```

or else

```julia
struct myParamFun{P}
       fun::Function
end
(pf::myParamFun{P})(x...) where P =pf.fun(P,x...)

```

Here `param` or `P` may be a `struct` with many fields.  
Are these proper ways to do what I have in mind? If not, what would be a better way of doing it? Hopefully the function should be GPU and / or AD friendly?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 14, 2023, 11:54pm UTC](https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656/2 "2023-02-14T23:54:53Z")

</div>

> [@jinfreedom](#):
>
> I have the need to define functions that takes arguments and parameters, where the latter often times don’t change for a given scenario

Often, this sort of thing is accomplished by constructing an anonymous function that “hides” the extra parameters, for example:

```julia
(x, y) -> myfunc(x, y, otherparams…)

```

which can be done on the fly as needed.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 14, 2023, 11:56pm UTC](https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656/3 "2023-02-14T23:56:40Z")

</div>

This is commonly referred to as [partial application](https://en.wikipedia.org/wiki/Partial_application). I would recommend not re-inventing the wheel, and instead using Julia’s built-in generic partial application type.

Which it doesn’t have, but it could look [like this](https://github.com/JuliaLang/julia/pull/24990#issuecomment-1386444366) one day. You will probably need to first wait for the heat death of the universe though.

Until then, you can always create anonymous functions as described above.

---

<div class="post-metadata">

**Author:** ![jinfreedom](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jinfreedom](https://discourse.julialang.org/u/jinfreedom)\
**Post date:** [February 15, 2023, 12:05am UTC](https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656/4 "2023-02-15T00:05:00Z")

</div>

Thanks for the feedback. I get that there is nothing non “julian” with anonymous functions. However, I did run into broadcast over `CuArray` problems if the parameters are not explicitly constant, even though they come from an immutable `struct`, as [this post](https://discourse.julialang.org/t/broadcast-over-cuarray-error/94654) indicates. Wondering if there is a way though …

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 15, 2023, 12:35am UTC](https://discourse.julialang.org/t/proper-way-to-define-functions-with-additional-parameters/94656/5 "2023-02-15T00:35:51Z")

</div>

Using an anonymous function, instead of

```julia
pf=myParamFun((x,y,z,v)->x+y-z*v,2)

```

you’d have

```julia
pf=(x...) -> ((x,y,z,v)->x+y-z*v)(x...,2)

```
