# How to properly define a function with changeable parameters within a struct?

**URL:** <https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 21, 2020, 12:44am UTC](https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292 "2020-08-21T00:44:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RuckerC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruckerc/32/16095_2.png) [@RuckerC](https://discourse.julialang.org/u/RuckerC)\
**Post date:** [August 21, 2020, 12:44am UTC](https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292/1 "2020-08-21T00:44:17Z")

</div>

Is there a way to define a function within a struct such that the function depends on some of the other fields in the struct? For example, I currently have

> mutable struct Net  
> a::Float64  
> b::Float64  
> f::Function  
> end
> 
> function Net()  
> a = rand()  
> b = rand() f(x, y) = a_x + b_y  
> Net(a, b, f)  
> end

Then if I create an instance of Net by `u = Net()` I can call the internal function` u.f(1.0, 2.0)`. With this approach if i update one of the fields, like `u.a = 0` , the function `u.f` uses the old parameter value for `a` . Is there a way to do this so that updating `a` or `b` would also update `f`?

---

<div class="post-metadata">

**Author:** ![lhnguyen-vn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lhnguyen-vn/32/15449_2.png) [@lhnguyen-vn](https://discourse.julialang.org/u/lhnguyen-vn)\
**Post date:** [August 21, 2020, 12:53am UTC](https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292/2 "2020-08-21T00:53:31Z")

</div>

Maybe the functor approach would be useful here

```julia
mutable struct Net
    a::Float64
    b::Float64
end

(n::Net)(x, y) = n.a * x + n.b * y

u = Net(rand(), rand())
u(1.0, 2.0)

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 21, 2020, 12:56am UTC](https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292/3 "2020-08-21T00:56:44Z")

</div>

this sounds like something you want to do with an OO language but not with Julia. There shouldn’t be “internal” function which “belongs” to certain `struct`.

I personally think it’s an anti-pattern to have a `function` as one of the field of a `struct` while some “self-modification” happen at the same time.

* * *

I think if you look at `Flux.jl`, a `model` is like your `Net`:

```julia
model = Chain(Dens(5,5), softmax)

```

the model has parameters, `params(model)`, but when updating parameters, you don’t do `model.train!(x, y)`, instead, the pattern is:  
`Flux.train!(opt, params(model), loss...)`

---

<div class="post-metadata">

**Author:** ![RuckerC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruckerc/32/16095_2.png) [@RuckerC](https://discourse.julialang.org/u/RuckerC)\
**Post date:** [August 21, 2020, 1:11am UTC](https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292/4 "2020-08-21T01:11:39Z")

</div>

Funny enough that is precisely what I am doing. I actually use the functor approach to define a neural network. I am looking at physics informed PDE and it is convenient to have derivatives of the NN w.r.t input arguments explicitly defined as functions. I was just getting tired of writing them out so I was hoping to make a NeuralNet struct that would hold everything for me.

---

<div class="post-metadata">

**Author:** ![RuckerC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruckerc/32/16095_2.png) [@RuckerC](https://discourse.julialang.org/u/RuckerC)\
**Post date:** [August 21, 2020, 1:12am UTC](https://discourse.julialang.org/t/how-to-properly-define-a-function-with-changeable-parameters-within-a-struct/45292/5 "2020-08-21T01:12:51Z")

</div>

Thank you, this definitely works but I have used this approach to define the feed-forward pass of my neural net and I am not sure I can use it to define the NN derivatives as well.
