# Variable not defined in module

**URL:** <https://discourse.julialang.org/t/variable-not-defined-in-module/43147>\
**Category:** General Usage\
**Tags:** question, module, scope\
**Created:** [July 15, 2020, 8:00pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147 "2020-07-15T20:00:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lewis\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis_liu/32/16400_2.png) [@Lewis\_Liu](https://discourse.julialang.org/u/Lewis_Liu)\
**Post date:** [July 15, 2020, 8:00pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147/1 "2020-07-15T20:00:16Z")

</div>

![Snipaste_2020-07-15_15-50-24](https://global.discourse-cdn.com/julialang/original/3X/f/f/ffad9498e8ac1064774739228e95eef55251bc4e.jpeg)

```julia
module TestN1
    sum=1
    for i in 1:10
       sum += i
    end
end

```

The code above doesn’t work. Is something wrong？  
Julia version 1.4.2.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [July 15, 2020, 8:55pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147/2 "2020-07-15T20:55:28Z")

</div>

Hi @Lewis_Liu and welcome to the julia Discourse! The reason your snippet doesn’t work is because modules in julia introduce global scope and loops introduce local scope. Local scopes inherit variables from enclosing local scopes but not from global scope. I imagine those last couple of sentences might have been pretty confusing if you’re not very experienced with variable scoping. I would recommend reading through the [section of the manual on variable scoping](https://docs.julialang.org/en/v1/manual/variables-and-scoping/) in order to get a thorough understanding, but here are a couple of quick examples to illustrate what I’m talking about:

If you want your example to work with minimal modification you need to use global variables:

```julia
module TestN1
    global sum=1
    for i in 1:10
      global sum += i
    end
end

```

However, that’s not necessary inside functions, which create their own local scope:

```julia
function testN1()
    sum=1
    for i in 1:10
       sum += i
    end
    return sum
end

```

In this case the function introduces a local scope and the for loop inherits variables from that enclosing scope.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 15, 2020, 8:57pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147/3 "2020-07-15T20:57:57Z")

</div>

Just complementing what @Pbellive said, I am not sure of what you are trying here. Because:

1. `sum` is already a name of a function in `Base`, and you are shadowing it. It is not a very good name to use, specially inside a module.
2. You are making a loop just inside a module, not inside a function inside the module, are you sure this is what you want?

---

<div class="post-metadata">

**Author:** ![Lewis\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis_liu/32/16400_2.png) [@Lewis\_Liu](https://discourse.julialang.org/u/Lewis_Liu)\
**Post date:** [July 15, 2020, 9:19pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147/4 "2020-07-15T21:19:50Z")

</div>

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Lewis\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis_liu/32/16400_2.png) [@Lewis\_Liu](https://discourse.julialang.org/u/Lewis_Liu)\
**Post date:** [July 15, 2020, 9:29pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147/5 "2020-07-15T21:29:33Z")

</div>

Thanks for complementing. I am learning about pre-compilation in Julia, my code is revised from a high voted answer in [StackOverflow](https://stackoverflow.com/a/40140349/2661337).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 15, 2020, 9:46pm UTC](https://discourse.julialang.org/t/variable-not-defined-in-module/43147/6 "2020-07-15T21:46:05Z")

</div>

Ah, ok, it makes sense. The objective is exactly to do nothing except waste time, so the difference of time in the precompilation and post-compilation times is easier to see. It is just kinda of a strange code to see outside of this context.
