# Explain scoping confusion to a programming beginner

**URL:** <https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 17, 2020, 4:12am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206 "2020-07-17T04:12:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 17, 2020, 4:12am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/1 "2020-07-17T04:12:01Z")

</div>

The scoping rules feel unintuitive, and I feel more confused after reading numerous Discourse and StackExchange responses, such as [Stefan’s explanation](https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514/9).

I come from MATLAB which has really straightforward and intuitive scoping rules, can someone explain why I should get different results between the two following scenarios? (I `include` them in a file at the REPL.)

```julia
bFound = false
for n = 1:10
    if n > 5
        bFound = true
        println("bFound = ", bFound)
        break
    end
end
println("bFound = ", bFound)

```

which yields

```julia
bFound = true
bFound = false

```

as opposed to

```julia
function Test()
    bFound = false
    for n = 1:10
        if n > 5
            bFound = true
            println("bFound = ", bFound)
            break
        end
    end
    println("bFound = ", bFound)
end

Test()

```

which yields

```julia
bFound = true
bFound = true

```

I just don’t understand the inner workings of programming languages enough to know why the best choice in the big picture of things results in obtaining different results here.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [July 17, 2020, 5:10am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/2 "2020-07-17T05:10:36Z")

</div>

> [@kapple](#):
>
> I just don’t understand the inner workings of programming languages enough to know why the best choice in the big picture of things results in obtaining different results here.

My suggestion: (1) stick with Jupyter for general scripting (which doesn’t have this issue); (2) put code in functions when you move to `.jl` files - which has other advantages. If you do that, you will never see this issue again.

---

<div class="post-metadata">

**Author:** ![kobied](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobied/32/16432_2.png) [@kobied](https://discourse.julialang.org/u/kobied)\
**Post date:** [July 17, 2020, 6:06am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/3 "2020-07-17T06:06:29Z")

</div>

As I understand it, at the first code, julia creates a local variable within the for-loop which is not related to the bFound outside of the loop.  
they may have the same name but they are different variables.

_**restricting the scope of a variable to local scope can be done by defining them  
within a function or a control construct. This way, we can use the same variable name more than once without name conflicts.**_

_ **functions support local variables, but for, while try, let, and type blocks can all support a local scope. Any variable defined in a for, while, try, or let block will be local unless it is used by an enclosing scope before the block.** _

in order to tell julia not to create local variable with in the for block you call do one of the following:

1. define bFound whith in the for block as **global** (this is highly unrecommended dou to performance isuase):

```julia
bFound = false
for n = 1:10
    if n > 5
        global bFound = true
        println("bFound = ", bFound)
        break
    end
end
println("bFound = ", bFound)

```

1. wrap the for block with **let** :

```julia
let bFound = false
    for n = 1:10
        if n > 5
            bFound = true
            println("bFound = ", bFound)
            break
        end
    end
    println("bFound = ", bFound)
end

```

1. wrap the for block with a function as you did in the second code, the function creates an enclosing scope, much like the **let** did.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [July 17, 2020, 7:17am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/4 "2020-07-17T07:17:07Z")

</div>

The behavior you are describing will change with Julia 1.5 (which will be released very soon):

[https://github.com/JuliaLang/julia/blob/release-1.5/NEWS.md#language-changes](https://github.com/JuliaLang/julia/blob/release-1.5/NEWS.md#language-changes)

---

<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:** [July 17, 2020, 12:19pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/5 "2020-07-17T12:19:44Z")

</div>

Demo of 1.5 behavior. In the REPL:

```julia
julia> bFound = false
false

julia> for n = 1:10
           if n > 5
               bFound = true
               println("bFound = ", bFound)
               break
           end
       end
bFound = true

julia> println("bFound = ", bFound)
bFound = true

```

Same code in a file:

```julia
┌ Warning: Assignment to `bFound` in soft scope is ambiguous
| because a global variable by the same name exists: `bFound`
| will be treated as a new local. Disambiguate by using
| `local bFound` to suppress this warning or `global bFound`
| to assign to the existing global variable.
└ @ string:4
bFound = true
bFound = false

```

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 26, 2020, 7:56am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/6 "2020-07-26T07:56:11Z")

</div>

Thanks everyone for your responses, they’ve definitely been helpful!

My original (convoluted) question remains somewhat unanswered, but I’ll take that as a hint that while there are reasons for having the strange behaviour of scoping, they’re mentally unattainable for a beginner.

That said, I’ll mark Stefan’s 1.5 prospective Julia behaviour as a solution anyway.

Thanks again everyone!

---

<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:** [July 26, 2020, 2:35pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/7 "2020-07-26T14:35:55Z")

</div>

The 1.0-1.4 rule is very simple:

- functions, loops, try blocks, structs, and comprehensions introduce local scope blocks
- when you see `x = ...` in a local scope:
  - if `x` is already a local, it assigns it
  - otherwise it creates a new local `x`

Is some part of this rule unclear? Or do you just find the result to be not what you’re used to? Intuitiveness and simplicity are often at odds with one another. Or put another way, mechanisms that humans find intuitive are often surprisingly complex and subtle.

---

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [July 26, 2020, 6:55pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/8 "2020-07-26T18:55:23Z")

</div>

I was typing out an example because I thought I knew where the confusion was coming from, but it turns out I don’t understand scoping either. Julia 1.4.2:

```julia
julia> module Foo
       x = 1
       println("From module Foo ", x)
       function f()
           println("From Foo.f ", x)
           println("From Foo.f after assignment ", x) # I know I haven't done any assignment here, see the next example for context
       end
       end
From module Foo 1
Main.Foo

julia> import .Foo

julia> Foo.f()
From Foo.f 1
From Foo.f after assignment 1

```

This works, but just add one more line and it doesn’t.

```julia
julia> module Foo
       x = 1
       println("From module Foo ", x)
       function f()
           println("From Foo.f ", x)
           x = 2
           println("From Foo.f after assignment ", x)
       end
       end
From module Foo 1
Main.Foo

julia> import .Foo

julia> Foo.f()
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] f() at ./REPL[1]:5
 [2] top-level scope at REPL[3]:1

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 26, 2020, 7:00pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/9 "2020-07-26T19:00:36Z")

</div>

That is the second bullet point about `x` now being assigned in local scope.

---

<div class="post-metadata">

**Author:** ![scimas](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@scimas](https://discourse.julialang.org/u/scimas)\
**Post date:** [July 26, 2020, 7:07pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/10 "2020-07-26T19:07:22Z")

</div>

So the error is about ambiguity of `x` and not `x` being undefined?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 26, 2020, 8:07pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/11 "2020-07-26T20:07:41Z")

</div>

There is no ambiguity. In your case, `x` is a local variable and you are trying to read it before it is defined. If you wanted to use the global `x` you write `global x`.

---

<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:** [July 26, 2020, 8:59pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/12 "2020-07-26T20:59:51Z")

</div>

One particular fact that might not match your intuition is that a given identifier (ie variable name) can only have one meaning in a given scope block: if it’s local anywhere in a block, then it’s local everywhere in that block. You might be expecting `x` to be global the first time you access it and then become local after it is assigned but that cannot happen. Since `x =` occurs in the function body, `x` is local to the function both before and after that assignment.

---

<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 27, 2020, 12:01am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/13 "2020-07-27T00:01:39Z")

</div>

> [@StefanKarpinski](#):
>
> functions, loops, try blocks, structs, and comprehensions introduce local scope blocks

And `let` blocks too. I use them extensively in `Jupyter` only because them introduce a local scope.

---

<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:** [July 27, 2020, 12:10am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/14 "2020-07-27T00:10:55Z")

</div>

Yep. I always lump them in with function bodies in my head but yes, those too.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 29, 2020, 12:54am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/15 "2020-07-29T00:54:46Z")

</div>

By now I understand the scoping rules I believe, and @scimas’ code makes sense to me, about Julia looking at the entire function first, instead of say Matlab which does some things statically and other things dynamically.

Speaking of Matlab, my confusion is just as to why the scoping rules for Julia are the way they are. I come from Matlab where the scoping rules make so much intuitive sense to me, and I’ve never had a problem with it. But knowing the ideologies of Julia creators, I have faith that there’s a bigger picture as the foundation for why the Julia scoping rules are the way they are.

My question _was_ two-fold, i.e. 1. How does scoping work and 2. What is the motivation behind such rules. #1 is answered, #2 is still in the air for me, with some unclear hints to the answer.

I apologize for not being clear in my long-winded post haha!

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [July 29, 2020, 3:45am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/16 "2020-07-29T03:45:57Z")

</div>

> [@kapple](#):
>
> By now I understand the scoping rules I believe, and @scimas’ code makes sense to me, about Julia looking at the entire function first, instead of say Matlab which does some things statically and other things dynamically.

I am not sure that is the right model to have. I think the right way to think about it is that Matlab has scripts where julia doesn’t (except, Jupyter notebooks, which can serve a similar purpose). This isn’t exactly true, but it is a close to truth to form a mental model. In julia, what look like scripts (e.g. the `.jl` files) aren’t. They are simply text which can be included like anything else into a session in whatever order you wish (and doing whatever you want in the middle, e.g. using the repl).

That is why in matlab in your `.m` files you have to manually say `global` on a variable, and everything else is local to the script. In julia, anything at the top level is a global.

In fact, the `global` keywords in the language are completely different. In matlab you declare a variable as global in your script because otherwise they are local to it, whereas in julia you provide the global in a function/loop/scope to tell the compiler you want to refer to a top-level variable rather than create a new local one.

> [@kapple](#):
>
> 1. How does scoping work

The main place that the “everything top level is global” gets you is in in the loops. This doesn’t happen in matlab because loops don’t introduce a new scope. This is actually a major benefit of Julia for bugs, reasoning about code, etc. but has this confusing scoping downside.

In Julia \<= v0.6, in Jupyter notebooks the whole time, and in the REPL julia \>= 1.5, there are special scoping behavior to make this downside basically non-existent. You will find working in Jupyter entirely intuitive, and soon the same for the REPL. For `jl` files, you will still want to wrap things in functions… but there are plenty of other reasons to do that.

> [@kapple](#):
>
> But knowing the ideologies of Julia creators,

> [@kapple](#):
>
> 1. What is the motivation behind such rules. #1 is answered, #2 is still in the air for me, with some unclear hints to the answer.

I don’t think there is any ideology here, just tradeoffs. The only ideological decision distinct from matlab is that loops/comprehensions/etc. introduce a scope - which I think is a good thing.

There are downsides in the scoping approach taken in the v0.6/jupyter/v1.5 REPL approach as well - though these typically confuse more advanced users rather than beginners. Rehashing those downsides wouldn’t be helpful, but to suffice it to say the chance in v0.7 in Julia was legitimate reasons.

> [@kapple](#):
>
> I apologize for not being clear in my long-winded post haha!

It is all very confusing, especially where people come from matlab where the `global` keyword means something completely different. The issue is was enough that the language designers brought things back to have consistent scoping in the REPL to match jupyter.

My suggestion is simple:

1. Use Jupyter for “scripts” where you are doing exploratory top-level code. And intend to copy/paste that code into functions eventually. In 1.5 you can do the same in the REPL as well.
2. Put all loops/etc. inside of functions in the `.jl` files
3. If you follow (1) and (2) you will **never see this issue again**. The scoping will be completely intuitive for normal stuff.
4. Forget everything just discussed in this thread. There are much more important things to learn and in in practice you won’t need to think about this unless you do very advanced programming in julia.

---

<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:** [July 29, 2020, 8:02am UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/17 "2020-07-29T08:02:25Z")

</div>

> [@kapple](#):
>
> why the scoping rules for Julia are the way they are

If you are interested really interested in the history, here are some links:

Way back in 2012, when the language was in its infancy, there was a discussion about scoping that eventually motivated “soft” and “hard” scopes, which Julia had until 0.6:

> <https://github.com/JuliaLang/julia/issues/423>
>
> Hello Julia developers,
> 
> congratulations with your great new language. I was sin…ging 'O Julia' out loud all day yesterday.
> 
> But now my bug report. Notwithstanding the \`let\`, \`global\` and \`local\` keywords, it seems there is not always a clear distinction in Julia between variable declaration and assignment. This breaks encapsulation and can lead to hard to find bugs.
> 
> \`\`\`
> function some\_scope()
> ...
> ... many lines of code
> ...
> f = () -\> x = 42 # Not clear if this declares a local variable or assigns to one from the enclosing scope.
> end
> \`\`\`
> 
> Matlab's scoping rules we don't even need to discuss \[1\], Coffeescript is a disaster \[2\], please don't copy Ruby on this one \[3\], Perl and Javascript are ok as long as you don't forget your \`my\`'s and \`var\`'s (or use strict), Python fixed it in version 3 \[4\], and one could say Scheme got it right the first time in 1970 \[5\].
> 
> What is the reasoning behind Julia's intricate scoping rules?
> 
> \[1\] http://www.mathworks.nl/help/techdoc/matlab\_prog/f4-39683.html#f4-73993
> \[2\] https://github.com/jashkenas/coffee-script/issues/712
> \[3\] http://www.rubyist.net/~matz/slides/rc2003/mgp00010.html
> \[4\] http://www.python.org/dev/peps/pep-3104/
> \[5\] http://news.ycombinator.com/item?id=3379962

This was by and large intuitive, but had some corner cases. Scope simplified conceptually for 1.0:

> <https://github.com/JuliaLang/julia/pull/19324>
>
> This PR examines the impact of deprecating (much of?) the distinction between ha…rd/soft scope. Instead it simply distinguishes between global and local scope. This means that all scope-blocks introduce the same type of scope (local), rather than distinguishing that toplevel functions have special, hard scope rules. I've updated the manual to try to show how this change would impact the user. The main change is that there would no longer be the concept of implicit globals computed from examining the module bindings. Instead the global/local computation would be purely syntactic. For example, take the following code snippet:
> 
> \`\`\`julia
> global x = 0
> for x = 1:10 end
> @show x
> \`\`\`
> 
> Under the current, this shows \`10\`, because \`x\` had a value before the for-loop.
> Under the new rules, this shows \`0\`, since the for-loop introduce a new local scope.
> 
> Making this code work as before would requiring declaring \`x\` to be a global inside the for-loop scope block:
> \`\`\`julia
> for x = 1:10; global x; end
> \`\`\`
> 
> Another option that this PR still permits is to make an assignment to x inside a begin/end block:
> \`\`\`julia
> begin
> x = 0
> for x = 1:10 end
> end
> @show x
> \`\`\`
> 
> The impact to base is small (two corrections to code that will be broken anyways when #265 is fixed).
> 
> The impact to tests is larger, as it takes a large number of unintentionally-global variables and causes them to emit a deprecation warning. I think the resulting changes are arguably beneficial, even if we don't decide to change the scoping rules, since they reduce the number of objects being kept around in global variables.

but some people still found it unintuitive:

> <https://github.com/JuliaLang/julia/issues/28789>
>
> \### Example 1
> 
> This came up with a student who upgraded from 0.6 to 1.0 direct…ly, so never even got a chance to see a deprecation warning, let alone find an explanation for new behavior:
> 
> \`\`\`julia
> julia\> beforefor = true
> true
> 
> julia\> for i in 1:2
> beforefor = false
> end
> 
> julia\> beforefor # this is surprising bit
> true
> 
> julia\> beforeif = true
> true
> 
> julia\> if 1 == 1
> beforeif = false
> end
> false
> 
> julia\> beforeif # Another surprise!
> false
> 
> julia\> function foo()
> infunc = true
> for i in 1:10
> infunc = false
> end
> @show infunc
> end
> foo (generic function with 1 method)
> 
> julia\> foo() # "I don't get this"
> infunc = false 
> \`\`\`
> 
> \### Example 2
> 
> \`\`\`julia
> julia\> total\_lines = 0
> 0
> 
> julia\> list\_of\_files = \["a", "b", "c"\]
> 3-element Array{String,1}:
> "a"
> "b"
> "c"
> 
> julia\> for file in list\_of\_files
> # fake read file
> lines\_in\_file = 5
> total\_lines += lines\_in\_file
> end
> ERROR: UndefVarError: total\_lines not defined
> Stacktrace:
> \[1\] top-level scope at ./REPL\[3\]:4 \[inlined\]
> \[2\] top-level scope at ./none:0
> 
> julia\> total\_lines # This crushs the students willingness to learn
> 0
> \`\`\`
> 
> I "get" why this happens in the sense that I think I can explain, with sufficient reference to the arcana in the manual about what introduces scopes and what doesn't, but I think that this is problematic for interactive use.
> 
> In example one, you get a silent failure. In example two, you get an error message that is very there-is-no-spoon. Thats roughly comparable to some Python code I wrote in a notebook at work today.
> 
> I'm not sure what the rules are in Python, but I do know that generally you can't assign to things at the global scope without invoking global. But at the REPL it does work, presumably because at the REPL the rules are different or the same logic as if they were all are in the scope of function is applied.
> 
> I can't language-lawyer the rules enough to propose the concrete change I would like, and based on Slack this isn't even necessarily perceived as an issue by some people, so I don't know where to go with this except to flag it.
> 
> Cross-refs:
> \#19324
> https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514
> https://stackoverflow.com/questions/51930537/scope-of-variables-in-julia

so

> <https://github.com/JuliaLang/julia/pull/33864>
>
> After thinking about it off and on for quite a while, @StefanKarpinski and I mor…e-or-less decided that the best way to change the REPL scope situation (if at all) is just to bring back what v0.6 did. That's what SoftGlobalScope and IJulia already do (albeit somewhat approximately; implementing it internally it's much easier to get it 100%), and seems less disruptive than introducing a \*third\* behavior.
> 
> This needs to be finished up but is ready to try. Please give it a whirl.
> 
> I'm not sure what the best interface to it is; it seemed easiest just to drop a special expression in the AST itself.
> 
> fixes #28789

was introduced to make a particular use case easier.

You will notice that a lot of thought went into scoping rules. If you want a deeper understanding, I recommend working through the code examples people posted in various discussions above (and some others you will find from there); I found it really instructive.

Generally it is not easy to define scoping rules that are intuitive (“do what I mean”), yet easy to reason about (including corner cases), especially in languages that don’t have different syntax for assignment and introducing new variables.

IMO the important thing is not whether scope behaves in a way that users from some other language will find intuitive, since Julia users come from variety of languages with different solutions to scope; but whether scoping is easy to understand and apply in practice after reading the relevant chapter in the manual. Personally, I think Julia’s current approach is a rather nice practical solution.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 29, 2020, 1:24pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/18 "2020-07-29T13:24:50Z")

</div>

> [@jlperla](#):
>
> Matlab has scripts where julia doesn’t

> [@jlperla](#):
>
> the `global` keywords in the language are completely different.

Ah that makes a lot of sense. I’m further coming to process that things that may look the same in different languages can be quite different.

> [@jlperla](#):
>
> I don’t think there is any ideology here, just tradeoffs.

That makes sense. When I mentioned ideologies, I _was_ referring to the more general purpose of Julia, to place the greater powers of programming into the hands of those who want easier ways to implement things while getting high performance out of it, particularly for large scale projects. And that filters down to the motivation for defining scoping rules regarding loops, which I have been asking about and can accept.

> [@jlperla](#):
>
> these typically confuse more advanced users rather than beginners

That’s an interesting observation, I was unaware of that, thanks for sharing.

> [@jlperla](#):
>
> There are much more important things to learn and in in practice you won’t need to think about this unless you do very advanced programming in julia.

Haha, yeah well I have quite a few questions in my experience with Julia that I haven’t chosen to ask about. I’ve just chosen this scoping one because it seems like other people have asked about it but the answers seemed pretty involved so I decided to ask for an answer for a beginner programmer, which is what I am.

Thanks again for your help! =)

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [July 29, 2020, 1:54pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/19 "2020-07-29T13:54:19Z")

</div>

> [@kapple](#):
>
> That’s an interesting observation, I was unaware of that, thanks for sharing.

Sorry if I was unclear, what I meant was that the downsides that @Tamas_Papp described in his response above - which triggered the change from Julia v0.6 change - are typically issues for more advanced programmers whereas the older behavior didn’t seem to confuse new users. “A priori”  
I don’t think this was obvious and it only became clear after the Julia 1.0 was released (since beginners understandably didn’t engage in the long beta/release candidate stage prior to the release).

Anyways, glad that things are starting to be more clear. What I can tell you is that other than the issue you have stumbled on, scoping in Julia is far superior to Matlab/Python in nearly every other respect - especially when it comes to the possibility of writing efficient code with fewer silent bugs.

---

<div class="post-metadata">

**Author:** ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)\
**Post date:** [July 29, 2020, 1:58pm UTC](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206/20 "2020-07-29T13:58:35Z")

</div>

Ah, thanks for clarifying that as well. I was wondering what was going on behind the scenes that you were referring to.

And yeah, I’ve started reading the discussions in the links @Tamas_Papp shared, and it’s expanding my understanding. The things available upon a Google search lacked context for me I suppose.

[Next page](https://discourse.julialang.org/t/explain-scoping-confusion-to-a-programming-beginner/43206.md?page=2)
