# How to pass variable into loop?

**URL:** <https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 24, 2019, 1:14pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602 "2019-06-24T13:14:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 24, 2019, 1:14pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/1 "2019-06-24T13:14:38Z")

</div>

Hello,  
I have created some variable in the mainstream of a Julia stream; in order to pass them (to be modified) inside a for loop, I first declared them as `global`.  
For instance;

```julia
global a = 10
for i in 1:100
  b = a*2
  a = b*0.1
end

```

give the error:  
`UndefvarError: a is not defined`  
If I define global inside the loop as well, I get:

```julia
global a = 10
for i in 1:100
  b = global a*2
  a = b*0.1
end
> syntax: invalid syntax in "global" declaration

```

if I define the variable as global before giving any value, I still get the undefined error:

```julia
global a = Int8[]
a = 10
for i in 1:100
  b = a*2
  a = b*0.1
end
> UndefVarError: a not defined

```

I am still confused on how to pass values from the main to the loops in Julia. Any help, please?  
Thank you.

---

<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:** [June 24, 2019, 1:26pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/2 "2019-06-24T13:26:42Z")

</div>

```julia
a = 10
for i in 1:100
  global a
  b = a*2
  a = b*0.1
end

```

---

<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:** [June 24, 2019, 1:38pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/3 "2019-06-24T13:38:40Z")

</div>

Or, as a puzzle for you:

```julia
julia> let
         a = 10
         for i in 1:1
           b = a*2
           a = b*0.1
           println(b)
           println(a)
         end
       end
20
2.0

julia> a
10

```

I admit, the documentation  
[https://docs.julialang.org/en/v1.3-dev/manual/variables-and-scoping/#Local-Scope-1](https://docs.julialang.org/en/v1.3-dev/manual/variables-and-scoping/#Local-Scope-1)  
could be clearer. I am somehow now used to this behaviour but the documentation is not easy to digest.

---

<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:** [June 24, 2019, 1:43pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/4 "2019-06-24T13:43:07Z")

</div>

Lots of deep thoughts about 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 …

and other threads.

I wouldn’t call it a debacle, actually I like it as it is or better: I like some of implications of it, e.g. one has to think about scope.

---

<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:** [June 24, 2019, 1:50pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/5 "2019-06-24T13:50:54Z")

</div>

When you are getting started, the easiest thing to do is work in jupyter for interactive “scripts”, and put things inside of functions when you are working in atom or the REPL. If you do that, it will work intuitively, and you don’t need `global`.

If you ever have to type `global` at that point, then you should probably rethink your code structure

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [June 24, 2019, 7:25pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/6 "2019-06-24T19:25:43Z")

</div>

Thank you. This is a bit counter-intuitive: I am used to declare the variables a global at the beginning and going into the loops. Here they are declared global INSIDE the loop and AFTER they have been assigned. Anyway I’ll treasure the tip for the next scripts!

---

<div class="post-metadata">

**Author:** ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)\
**Post date:** [June 24, 2019, 7:36pm UTC](https://discourse.julialang.org/t/how-to-pass-variable-into-loop/25602/7 "2019-06-24T19:36:31Z")

</div>

I wouldn’t think of it that way. The variable is automatically global if you define it in the REPL outside a function. You just need to use the `global` keyword to modify a global in a loop.  
For example,

```julia
julia> a = 10
10
julia> for i in 1:100
          b = a*2
         a = b*0.1
       end
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] top-level scope at ./REPL[117]:2 [inlined]
 [2] top-level scope at ./none:0

julia> for i in 1:100
          b = a*2
         global a = b*0.1
       end

```

If you use functions or jupyter you won’t have to worry about this.
