# Variable which disappear in a for

**URL:** <https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304>\
**Category:** General Usage\
**Tags:** question, loops\
**Created:** [November 17, 2020, 3:48pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304 "2020-11-17T15:48:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dailysight](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Dailysight](https://discourse.julialang.org/u/Dailysight)\
**Post date:** [November 17, 2020, 3:48pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/1 "2020-11-17T15:48:03Z")

</div>

Hey everyone !  
First of all, i would like to thank everyone of this community for the help you give to beginners like me.  
Actually, i have a problem about a variable which disappear when i enter a for loop.  
Does anyone has an idea why ?

The following code :

```julia
a = 0

for i in 0:0.1:pi/2

    a = a + 1;

    θ[1] = i;

end 

```

LoadError: UndefVarError: a not defined

Kind Regards

---

<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:** [November 17, 2020, 4:25pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/2 "2020-11-17T16:25:00Z")

</div>

see here:

> [@Julia's loop scope behavior differs from Fortran?](https://discourse.julialang.org/t/julias-weird-behavior-of-variables-in-for-loops/50275/23):
>
> EDIT: I get the exact same performance between them (and -march=native doesn’t seem to matter). Compiling with gfortran -O3 -march=native rootloop.f90 -shared -fPIC -o libloop.so gfortran -Ofast -march=native rootloop.f90 -shared -fPIC -o libloopfast.so Yields: julia\> using BenchmarkTools julia\> function loop(x,n) s = 0. for i in 1:n r = sqrt(i) s = s + x\*r end s end loop (generic function with 1 method) julia\> function loop…

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 17, 2020, 4:32pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/3 "2020-11-17T16:32:20Z")

</div>

Julia has different rules for scoping than what you are used to. See the docs [here](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope).

Solution 1: Decalre `a` global in the loop

```julia
a = 0
for i in 1:5
    global a
    a += 1
end

```

Solution 2: Use a `let` block

```julia
let a = 0
    for i in 1:5
        a += 1
    end
    a
end

```

Solution 3: Use a function

```julia
function foo()
    a = 0
    for i in 1:5
        a += 1
    end
    a
end

```

In general, solution 3 is the best. You should strive to put _all_ your code in functions. At the REPL, in interactive use, the `for` loop `variable` behavior is the same as in a function. This is for convenience only, and scripts in global scope don’t have the same behavior.

---

<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:** [November 17, 2020, 5:09pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/4 "2020-11-17T17:09:23Z")

</div>

> [@Dailysight](#):
>
> beginners like me.

If you stick in Jupyter or using the shift-enter in vscode, or in the REPL, things will behave as you are expecting. This is only an issue if you are running them in `.jl` files. For beginners, it is generally better to start with interactive usage in Jupyter/etc… Then, by the time you want to put loops in files, you will always want them in functions (for a variety of reasons, including performance). If you do this, you will never see this issue again.

---

<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:** [November 17, 2020, 5:48pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/5 "2020-11-17T17:48:06Z")

</div>

In the last case, if you gonna use a file for a simple script, I would suggest developing the habit of putting the whole script inside a `main` function and calling it.

```julia
function main()
    # all code that was directly in global scope  
end
main()

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 17, 2020, 5:57pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/6 "2020-11-17T17:57:56Z")

</div>

Also note that if you update to Julia 1.5 or above then your example will work at the REPL as intended (except that you have not defined `θ` in the code you provided).

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 17, 2020, 7:32pm UTC](https://discourse.julialang.org/t/variable-which-disappear-in-a-for/50304/7 "2020-11-17T19:32:17Z")

</div>

This + heavy use of Infiltrator.jl and Exfiltrator.jl is the key to doing data analysis in julia imo.
