# Constructing a function at runtime to pass as an argument

**URL:** <https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909>\
**Category:** New to Julia\
**Created:** [February 9, 2021, 9:34am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909 "2021-02-09T09:34:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 9:34am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/1 "2021-02-09T09:34:30Z")

</div>

HI,

I’m trying to create a function at runtime to pass as a parameter to another function (Agents.random\_agent to be exact). I can pass a condition but the condition is dependant on the state of the current agent so I need to somehow add that.

In case this all sounds confusing, here’s what I’m trying to do:

The random\_agent function is defined as follows:

```julia
random_agent(model, condition)

```

I have defined the agents as follows (simplified version of actual code):

```julia
struct MyAgent # <: AbstractAgent - this is only useful when working with the actual agents but doesn't really matter here.
    id::Int
    state::Int
    triggers_on_state::Int
end

function foo(model, agent::MyAgent)
    # construct a function f(agent) that returns true when an agent's triggers_on_state equal the state of the agent passed to foo.
    an_agent = random_agent(model, f)
end

```

How do I construct f in such a way I can construct a different f on each call of foo()?

Thanks in advance,  
Stef

---

<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:** [February 9, 2021, 10:32am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/2 "2021-02-09T10:32:14Z")

</div>

Could `f` receive two parameters and be passed like a closure?

```julia
f(agent,state) = ....
an_agent = random_agent(model, state -> f(agent, state))

```

Then inside `random_agent` you can check the condition, without having to define the function at runtime.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 10:33am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/3 "2021-02-09T10:33:55Z")

</div>

The problem is that random\_agent is not my code, it’s part of a package.

---

<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:** [February 9, 2021, 10:37am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/4 "2021-02-09T10:37:04Z")

</div>

Uhm, that is not necessarily an impediment to the approach above.

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 10:39am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/5 "2021-02-09T10:39:33Z")

</div>

Ah, I see. I haven’t worked with closures yet and made a wrong assumption. Is there some more in depth documentation on closures somewhere? The chapter in the reference manual is a bit too dense for me.

Thanks!

---

<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:** [February 9, 2021, 10:48am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/6 "2021-02-09T10:48:55Z")

</div>

I have some personal notes here, maybe they are helpful: [https://m3g.github.io/JuliaNotes.jl/stable/closures/](https://m3g.github.io/JuliaNotes.jl/stable/closures/)

---

<div class="post-metadata">

**Author:** ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)\
**Post date:** [February 9, 2021, 10:49am UTC](https://discourse.julialang.org/t/constructing-a-function-at-runtime-to-pass-as-an-argument/54909/7 "2021-02-09T10:49:24Z")

</div>

Thanks!
