# Why step function doesn't recognise local model initialisation inside a function?

**URL:** <https://discourse.julialang.org/t/why-step-function-doesnt-recognise-local-model-initialisation-inside-a-function/117586>\
**Category:** Modelling & Simulations\
**Tags:** question, agents, agentsjl, eventqueueabm\
**Created:** [July 29, 2024, 9:10am UTC](https://discourse.julialang.org/t/why-step-function-doesnt-recognise-local-model-initialisation-inside-a-function/117586 "2024-07-29T09:10:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 29, 2024, 9:10am UTC](https://discourse.julialang.org/t/why-step-function-doesnt-recognise-local-model-initialisation-inside-a-function/117586/1 "2024-07-29T09:10:28Z")

</div>

Here is my function where I try to run ABM model inside a function. Now inside this function when I initialise model it works and also prints model but while using `step!()` it says model is not defined. Can someone shed light? why `step!()` not recognising model defined inside function ? @Datseris

```julia
### No global initialisation of model 

function runn(i)
    
    model = random_model() # model definition
    println(model) # prints perfectly

    step!(model, step_point) # I get error here ---- model not defined 

end

for i in 1:4

    runn(i)

end

```

Output:

```julia
ERROR: UndefVarError: `model` not defined

```

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [July 29, 2024, 9:29am UTC](https://discourse.julialang.org/t/why-step-function-doesnt-recognise-local-model-initialisation-inside-a-function/117586/2 "2024-07-29T09:29:15Z")

</div>

Please refer to my comment here:

> [@Running EBM model in a function](https://discourse.julialang.org/t/running-ebm-model-in-a-function/117512/2):
>
> As usual, please provide a complete MWE, full stacktrace, and a proper description of your experienced issue and the expected behaviour.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 29, 2024, 9:29am UTC](https://discourse.julialang.org/t/why-step-function-doesnt-recognise-local-model-initialisation-inside-a-function/117586/3 "2024-07-29T09:29:39Z")

</div>

See [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) , especially the point about **making a minimal working example**. What’s `random_model()`?

p.s.: Please don’t tag me to your posts. I do not know you on a personal level and tagging someone happens on an individual level.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [July 29, 2024, 9:33am UTC](https://discourse.julialang.org/t/why-step-function-doesnt-recognise-local-model-initialisation-inside-a-function/117586/4 "2024-07-29T09:33:45Z")

</div>

ohh …sorry 😅 I didn’t know we can’t tag package developers. I will try to make a MWE and post here. 🙂
