# Is there anybody know why this kind of simple code cannot be run?

**URL:** <https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 8, 2021, 11:00pm UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795 "2021-12-08T23:00:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [December 8, 2021, 11:00pm UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795/1 "2021-12-08T23:00:22Z")

</div>

```julia
function test_reason()
    for i = 1:10
        if i == 1
            x = 1
        else 
            x = x+1
        end
    end

end

test_reason()

```

It seems that it has some scope problem but it’s strange that I have alread define a fucntion for it.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [December 8, 2021, 11:08pm UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795/2 "2021-12-08T23:08:30Z")

</div>

See the [loop scoping rules](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Loops-and-Comprehensions), in particular this part:

> [N]ew variables introduced in their body scopes are freshly allocated for each loop iteration

You need to define `x` outside the loop if you want it to persist beyond each loop iteration.

```julia
julia> function test_reason()
           local x
           for i = 1:10
               if i == 1
                   x = 1
               else
                   x = x+1
               end
           end
           return x
       end
test_reason (generic function with 1 method)

julia> test_reason()
10

```

---

<div class="post-metadata">

**Author:** ![Lyupinpin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lyupinpin/32/9295_2.png) [@Lyupinpin](https://discourse.julialang.org/u/Lyupinpin)\
**Post date:** [December 8, 2021, 11:16pm UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795/3 "2021-12-08T23:16:28Z")

</div>

That’s helpful.  
Thank you.

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [December 8, 2021, 11:47pm UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795/4 "2021-12-08T23:47:03Z")

</div>

Or

```julia
function test_reason()
           x = 1
           for i = 1:10
               if i > 1
                   x = x+1
               end
           end
           return x
       end

```

---

<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:** [December 9, 2021, 12:41am UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795/5 "2021-12-09T00:41:45Z")

</div>

I think I gave a similar question a good answer in the past: [I thought I understood "pass-by-sharing" in Julia until I found this - #19 by Henrique\_Becker](https://discourse.julialang.org/t/i-thought-i-understood-pass-by-sharing-in-julia-until-i-found-this/46897/19).

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 9, 2021, 6:24am UTC](https://discourse.julialang.org/t/is-there-anybody-know-why-this-kind-of-simple-code-cannot-be-run/72795/6 "2021-12-09T06:24:43Z")

</div>

Even simpler:

```julia
function test_reason()
    x = 0
    for i = 1:10
        x = x+1
     end
     return x
end

```

Or (though I expect this is just due to an overly M MWE):

```julia
test_reason() = 10

```
