# Puzzled by scope rules

**URL:** https://discourse.julialang.org/t/puzzled-by-scope-rules/54103
**Category:** New to Julia
**Created:** [January 28, 2021, 8:25am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103 "2021-01-28T08:25:48Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [January 28, 2021, 8:25am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/1 "2021-01-28T08:25:48Z")

</div>

When running

```julia
function ten()
    j = 1
    while j < 10
        global j += 1
    end
end
println(ten())

```

one gets

```julia
ERROR: LoadError: UndefVarError: j not defined

```

Which would be the correct way to define `j`? The block inside the function runs fine. Thanks in advance.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [January 28, 2021, 8:31am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/2 "2021-01-28T08:31:37Z")

</div>

There is no global j defined, e.g. this would solve the error, but result in an infinit loop:

```julia
j=1
function ten()
    j = 1
    while j < 10
        global j += 1
    end
end
println(ten())

```

You can translate it into:

```julia
global_j=1
function ten()
    local_j = 1
    while local_j < 10
        global global_j += 1
    end
end
println(ten())

```

Where you see why it never stops.

---

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [January 28, 2021, 8:57am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/3 "2021-01-28T08:57:31Z")

</div>

Thank you for your reply. This works

```julia
function ten()
    global j = 1
    while j < 10
        global j += 1
    end
    return j
end
println(ten())

```

but is not very pretty!

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [January 28, 2021, 8:59am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/4 "2021-01-28T08:59:32Z")

</div>

Of course it’s not pretty, and why do you use global in your code above? It would work just without global.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [January 28, 2021, 9:10am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/5 "2021-01-28T09:10:09Z")

</div>

> [@askvorts](#):
>
> ```julia
> function ten()
> j = 1
> while j < 10
> global j += 1
> end
> end
> println(ten())
> 
> ```

I think @oheil is explaining how you could use globals in this code. But you _shouldn’t_ use globals here _at all_. Do this instead:

```julia
function ten()
    j = 1
    while j < 10
        j += 1 # no global!
    end
end
println(ten())

```

---

<div class="post-metadata">

### Author: ![askvorts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/askvorts/32/7120_2.png) [@askvorts](https://discourse.julialang.org/u/askvorts)
#### Post date: [January 28, 2021, 9:18am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/6 "2021-01-28T09:18:27Z")

</div>

Thank you. I am learning Julia reading the manual. It all started in the section about Loops where is given the following example:

```julia
julia> i = 1;
julia> while i <= 5
           println(i)
           global i += 1
       end

```

I decided to see what would happen if this code block was inside a function…

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [January 28, 2021, 9:21am UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/7 "2021-01-28T09:21:37Z")

</div>

Actually I didn’t explained why the error comes up. See:

```julia
function ten()
    j = 1
    while j < 10
        global j += 1
    end
end
println(ten())

```

I translate it again:

```julia
function ten()
    local_j = 1
    while local_j < 10
        global global_j
        global_j = global_j + 1
    end
end
println(ten())

```

The undefined j is the bold one in: global\_j = **global\_j** + 1

---

<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: [January 28, 2021, 12:48pm UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/8 "2021-01-28T12:48:41Z")

</div>

> [@askvorts](#):
>
> Thank you. I am learning Julia reading the manual. It all started in the section about Loops

I think it is great that you are reading the manual, many questions we answer here are asked because people do not make this investment. In your case, the answer is in the next section [Scope of variables](https://docs.julialang.org/en/v1/manual/variables-and-scoping/). Unfortunately we have kinda of a deadlock here: we want the manual teach to control flow before scope, so we can use `for` loops in the scope section; however, because we did not explain scope first, all examples in the [Control Flow](https://docs.julialang.org/en/v1/manual/control-flow/) section use `global` variables direct in the REPL (and do not exactly explain why).

@Tamas_Papp You were doing a manual rewrite. Do you think this is something to consider? (Changing the order of the sections or making some adaptation.) I find kinda worrying that newbies are taught how to use `global` variables inside a loop before understanding how scope works. Maybe I could give a look at it this weekend.

---

<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: [January 28, 2021, 1:03pm UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/9 "2021-01-28T13:03:32Z")

</div>

Generally improvements to the manual are always very welcome, but I am not sure that a strictly linear reading order (like a novel) is feasible; a lot of things are interconnected. IMO the manual is best read multiple times.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 28, 2021, 1:31pm UTC](https://discourse.julialang.org/t/puzzled-by-scope-rules/54103/10 "2021-01-28T13:31:41Z")

</div>

I have some notes that may help understanding how these scope rules work: [Scope of loops · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/loopscopes/)

They of course do not bring nothing new that the manual does not cover.
