# Global variables not visible within loops in scripts

**URL:** <https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849>\
**Category:** General Usage\
**Created:** [October 3, 2018, 9:54pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849 "2018-10-03T21:54:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Ortley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_ortley/32/7845_2.png) [@David\_Ortley](https://discourse.julialang.org/u/David_Ortley)\
**Post date:** [October 3, 2018, 9:54pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/1 "2018-10-03T21:54:52Z")

</div>

All:

If I have a simple script that sets a variable at the top level, and then tries to modify the variable from within a loop, I get an error stating that the variable is not defined:

```julia
#!/usr/bin/env julia

i = 0
for j in 1:3
    i = i+1
    println(i, " ", j)
end

```

yields:

```julia
~$ ./test.jl
ERROR: LoadError: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope at /home/dortley/test.jl:5 [inlined]
 [2] top-level scope at ./none:0
 [3] include at ./boot.jl:317 [inlined]
 [4] include_relative(::Module, ::String) at ./loading.jl:1041
 [5] include(::Module, ::String) at ./sysimg.jl:29
 [6] exec_options(::Base.JLOptions) at ./client.jl:229
 [7] _start() at ./client.jl:421
in expression starting at /home/dortley/test.jl:4

```

If I do the same code, but wrap it in a function, I get the behavior I expect. So the following works just fine:

```julia
#!/usr/bin/env julia

function go()
    i = 0
    for j in 1:3
        i = i+1
        println(i, " ", j)
    end
end

go()

```

Is this a bug, or are we not expected to declare globals at the top level of a script like that?

Thanks.  
-David Ortley

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 3, 2018, 9:56pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/2 "2018-10-03T21:56:06Z")

</div>

In 1.0, you need to write `global i = i + 1`. Otherwise the for loop has a local called `i` which shadows the global variable `i`.

---

<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:** [October 3, 2018, 10:22pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/3 "2018-10-03T22:22:35Z")

</div>

can we make this `global` here the default behavior? if not, is this due to performance concern or we think this is more useful/intuitive.

I think python/c++ in this case would imply `i` comes from outer scope.

---

<div class="post-metadata">

**Author:** ![RaulDurand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rauldurand/32/2044_2.png) [@RaulDurand](https://discourse.julialang.org/u/RaulDurand)\
**Post date:** [October 4, 2018, 11:34am UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/4 "2018-10-04T11:34:07Z")

</div>

This has been discussed here: [https://github.com/JuliaLang/julia/issues/28789](https://github.com/JuliaLang/julia/issues/28789)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 4, 2018, 12:27pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/5 "2018-10-04T12:27:45Z")

</div>

> **[GitHub - JuliaLang/SoftGlobalScope.jl: utilities for "soft" global scope in...](https://github.com/JuliaLang/SoftGlobalScope.jl)**
>
> utilities for "soft" global scope in interactive Julia environments - GitHub - JuliaLang/SoftGlobalScope.jl: utilities for "soft" global scope in interactive Julia environments

---

<div class="post-metadata">

**Author:** ![David\_Ortley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_ortley/32/7845_2.png) [@David\_Ortley](https://discourse.julialang.org/u/David_Ortley)\
**Post date:** [October 4, 2018, 5:14pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/6 "2018-10-04T17:14:52Z")

</div>

Thanks for the replies.

So I read through the discussion on the issue posted by @RaulDurand and I think I follow. I’d like to summarize below to see if I understand the discussion correctly and also for the benefit of others:

- For the REPL and scripts, global variables aren’t seen inside of top level loops because of the possible ambiguity between local variables.
- Global variables ARE seen inside of `if` statements without the use of `global` (any others situations?)
- The behavior is intended.
- It is understood by the developers that there is some friction with that decision, no need for more pontification, even if well meaning. 🙂
- Currently, one can use `[SoftGlobalScope.jl](https://github.com/stevengj/SoftGlobalScope.jl)` or wrap stuff in a local scope such as a `let` block or function.
- Something like `SoftGlobalScope.jl` will get implemented in Base for v1.1, though no change in language behavior is planned through all of v1.x

Did I get everything correct?

-David Ortley

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 5, 2018, 4:36am UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/7 "2018-10-05T04:36:55Z")

</div>

Well… There’s been some more discussion since a few hours ago 😅

> [@Another possible solution to the global scope debacle](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894):
>
> The infamous issue [https://github.com/JuliaLang/julia/issues/28789](https://github.com/JuliaLang/julia/issues/28789) has been on most of our minds for a while now. Although I’ve stopped posting there, I can tell you I’ve been thinking about it a lot, as have Stefan and Jameson and others, and we have continued discussing it off and on. We should really try to get it right. Last time around, we more-or-less settled on changing only the REPL, which has the main advantage of being non-breaking for most code. But after sleeping on it, it does not …

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [October 16, 2019, 7:26pm UTC](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849/8 "2019-10-16T19:26:23Z")

</div>

Fighting with my code by running individual blocks of code to manually debug the logic of the program, until I discover a new thing in Julia. 😁  
**For loops have scopes too!**
