# Defining a variable within a function at the module scope?

**URL:** <https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114>\
**Category:** General Usage\
**Created:** [May 23, 2018, 10:13pm UTC](https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114 "2018-05-23T22:13:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [May 23, 2018, 10:13pm UTC](https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114/1 "2018-05-23T22:13:59Z")

</div>

Hi,

I was wondering how can i define a variable at the module scope within a function or code block.  
Is my only option “global” keyword?  
or is there function define(MODULE,sym)

Thanks

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [May 23, 2018, 10:24pm UTC](https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114/2 "2018-05-23T22:24:33Z")

</div>

Why do you need another option, and what is wrong with using `global`?

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [May 23, 2018, 10:32pm UTC](https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114/3 "2018-05-23T22:32:27Z")

</div>

i am just asking…

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 24, 2018, 5:20am UTC](https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114/4 "2018-05-24T05:20:11Z")

</div>

You could use `eval`, as that always operates in global scope, but that is even “worse” than `global`.

---

<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 24, 2018, 5:32am UTC](https://discourse.julialang.org/t/defining-a-variable-within-a-function-at-the-module-scope/11114/5 "2018-05-24T05:32:38Z")

</div>

It would be better if you explained what you are trying to accomplish.

You should never _define_ a global variable within a function, only _access_ it. That’s what `global` is for. In practice, if you want to provide type information to the compiler, you would use a container anyway, so you may not even need that. For example,

```julia
module Foo

const v = [1.0]

function change(Δ)
    v[1] += Δ
end

end

```
