# Confused about global vs local scoping in for loops in 1.0

**URL:** <https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318>\
**Category:** New to Julia\
**Created:** [October 14, 2018, 7:16pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318 "2018-10-14T19:16:08Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [October 14, 2018, 7:16pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/1 "2018-10-14T19:16:08Z")

</div>

This global vs local scoping really has me confused.

I understood that for loops have their own scope, so I was expecting this to error out, but it doesn’t.

```julia
julia> temp = 10
10

julia> for i in 1:5
           println(temp)
       end
10
10
10
10
10

```

Since I didn’t declare the variable temp inside the for loop, why didn’t this error out?

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [October 14, 2018, 7:24pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/2 "2018-10-14T19:24:54Z")

</div>

Here `temp` is a global variable and in Julia you can read from global variables anywhere (in the same module - here Main) just not assign to them. If you want to assign to a global variable then you need to declare it with a `global` statement.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [October 14, 2018, 7:28pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/3 "2018-10-14T19:28:19Z")

</div>

From [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#man-scope-table-1:)

_"In a local scope, all variables are inherited from its parent global scope block unless:_

_\* an assignment would result in a modified global variable, or_  
_\* a variable is specifically marked with the keyword `local` ._

_Thus global variables are only inherited for reading but not for writing: [snip]_  
_An explicit `global` is needed to assign to a global variable: [snip]"_

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [October 14, 2018, 7:29pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/4 "2018-10-14T19:29:13Z")

</div>

OK, I think I see. what is outside the for loop is defaulted to global, but what is defined within the for loop is local so after the loop completes, I cannot get those variables out, but I can get upstream global variables into the for loop to use.

I know there is another whole topic on this, but this is really unintuitive for people coming from other scripting languages.

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [October 14, 2018, 7:31pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/5 "2018-10-14T19:31:09Z")

</div>

> [@jonjilla](#):
>
> I know there is another whole topic on this, but this is really unintuitive for people coming from other scripting languages.

I agree. But it is really complicated. Difficult to decide for which use case to optimize.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [October 14, 2018, 7:33pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/6 "2018-10-14T19:33:07Z")

</div>

Yes, a number of people think similarly and so I think it’s likely to change in the (near-ish?) future if I read the other thread correctly.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [October 14, 2018, 7:38pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/7 "2018-10-14T19:38:59Z")

</div>

Not wanting to add more noise to this topic, but just how I think about this.

A function is like a black box so variables go in (through the function arguments) and variables go out through return statements, so that’s makes sense that functions that their own scope.

But for, try, while blocks don’t really have an “input port” and neither do they have an “output port”. It would be different if there were something like a return statement to get stuff processed in those objects out but I don’t think there is, is there (other than setting those variables as global). It’s kind of like creating a black box with only a clear mechanism to get things in but not out.

What about If/else blocks. Why don’t those have local scoping rules similar to for/while/try?

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [October 14, 2018, 8:18pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/8 "2018-10-14T20:18:38Z")

</div>

> [@jonjilla](#):
>
> creating a black box with only a clear mechanism to get things in but not out.

I don’t understand this statement. If you write `global temp = 20` within the for-loop then there is a very clear mechanism to get things out, isn’t it?

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [October 14, 2018, 8:24pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/9 "2018-10-14T20:24:39Z")

</div>

I mentioned, “other than setting variables as global” in my statement. But that feels like a workaround and seems kind of heavy handed. It feels like there should be another way other than resorting to globals.

Anyways, I don’t think I’m going to add anything useful to this discussion. I don’t have enough experience or background on programming languages. I’m just a simple user.

Anyways, thanks for taking the time to reply to this lowly user (me). I guess I’ll figure this out eventually.

---

<div class="post-metadata">

**Author:** ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)\
**Post date:** [October 14, 2018, 8:32pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/10 "2018-10-14T20:32:12Z")

</div>

…I’m also ‘just a simple user’ 😉

---

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [October 15, 2018, 7:46am UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/11 "2018-10-15T07:46:45Z")

</div>

I also find the new policy about global scopes and for loops quite complicated and confusing. Sure it can be justified (everything can, in the end), but would really like to change it back to what it was before the big changes of 0.7.0 (my two cents).  
Still somebody here pointed out that there is a big thread discussing that… can somebody please tell me which one is it? This forum is quite busy and it is sometimes hard to keep up with everything…  
Best regards and thanks,  
Ferran.

---

<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:** [October 15, 2018, 7:50am UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/12 "2018-10-15T07:50:45Z")

</div>

> [@Ferran\_Mazzanti](#):
>
> Still somebody here pointed out that there is a big thread discussing that… can somebody please tell me which one is it?

> [@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 …

is one of the latest ones.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [October 15, 2018, 10:53am UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/13 "2018-10-15T10:53:41Z")

</div>

> [@jonjilla](#):
>
> OK, I think I see. what is outside the for loop is defaulted to global, but what is defined within the for loop is local so after the loop completes, I cannot get those variables out, but I can get upstream global variables into the for loop to use.

It is a little more complicated!

```julia
julia> for i in 1:1
         temp = 2 # this is local
         for j in 1:1
            temp = 3 # this is not defaulted to global! 
         end
         println(temp) # local is changed
       end
       println(temp) # global is undefined
3
ERROR: UndefVarError: temp not defined

```

Same if `for` is inside function body where also inherit function’s local scope.

And **maybe** this is bug:

```julia
julia> temp = 1
       for i in 1:1
         temp = 2
         for j in 1:1
            global temp = 3 # you could not reach global `temp` here! 
         end
         println(temp)
       end
       println(temp)
ERROR: syntax: `global temp`: temp is a local variable in its enclosing scope

```

---

<div class="post-metadata">

**Author:** ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)\
**Post date:** [October 23, 2018, 6:22pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/14 "2018-10-23T18:22:52Z")

</div>

Keep simple things simple by default…

---

<div class="post-metadata">

**Author:** ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)\
**Post date:** [October 23, 2018, 6:36pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/15 "2018-10-23T18:36:14Z")

</div>

see also [Understanding while loop UndedVarError and scope - #14 by kristoffer.carlsson](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/14)

> [@jonjilla](#):
>
> What about If/else blocks. Why don’t those have local scoping rules similar to for/while/try?

Good question

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [November 5, 2018, 7:55pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/16 "2018-11-05T19:55:09Z")

</div>

You can greatly simplify things by writing functions for everything. Pass in everything the function needs as a parameter ( wrap them in tuples or struct if there are too many. ). For complex return values, use tuples and structs as well.  
In general use struct and not mutable struct as much as possible.  
I’ve happily never hit the problem with scoping and look on bemusedly at the amazingly long discussions on an issue I didn’t know existed.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 5, 2018, 8:41pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/17 "2018-11-05T20:41:47Z")

</div>

> [@Orbots](#):
>
> You can greatly simplify things by writing functions for everything.

This is basically telling people not to use Julia interactively.

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [November 5, 2018, 10:55pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/18 "2018-11-05T22:55:27Z")

</div>

Interactively. That’s how I use Julia. I develop extensively using the repl. Julia easily has the nicest repl-based function writing experience I’ve seen.

What do you mean by interactively?

By interactively I’m guessing you mean manipulating global state via expressions instead of functions?

Not sure why you would want to do that. So many pitfalls. There is a wealth of higher order functions available with very nice syntax. Broadcasting for example does an amazing amount. map reduce filter, it’s all there. Super simple anonymous functions. I do most of my work with hof.

Sure every now and then I interactively modify some global state directly, but it’s almost always pretty simple stuff setting up data for a function to process.

Function closures go a long way as well.

```
function thinkofname()
   M[1,1] = 0
   M[2,2] = 0
end

```

if you don’t like thinking of names

```
(()-> begin
   M[1,1] = 0
   M[2,2] = 0
   whatever expressions block you would normally enter
end)()

```

use ans immediately to evaluate your lambda expression with a closure mutating.

```
julia> A = rand(10,2)
10×2 Array{Float64,2}:
 0.674215 0.679194
 0.946359 0.26367 
 0.328985 0.119864
 0.793516 0.613542
 0.0598779 0.407411
 0.637759 0.233309
 0.172873 0.222074
 0.650882 0.91787 
 0.217435 0.8771  
 0.141181 0.853914

julia> ()->
   for i in 1:10
      A[i,1] = i
   end
#41 (generic function with 1 method)

julia> ans()

julia> A
10×2 Array{Float64,2}:
  1.0 0.679194
  2.0 0.26367

```

---

<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:** [November 5, 2018, 11:34pm UTC](https://discourse.julialang.org/t/confused-about-global-vs-local-scoping-in-for-loops-in-1-0/16318/19 "2018-11-05T23:34:16Z")

</div>

This thread really does not require any more discussion.
