# Variabile scope into the for? panic

**URL:** <https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971>\
**Category:** New to Julia\
**Created:** [January 30, 2020, 10:00am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971 "2020-01-30T10:00:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 10:00am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/1 "2020-01-30T10:00:35Z")

</div>

I am trying Julia since 3 weeks, so i am totally beginner. Here i was trying to get argument passed to my script, running into the command line:

> **folder\_path** = “”  
> println(“Set parameters …”)  
> for i in 1:length(ARGS)  
> x = ARGS[i]  
> println(“x=”\*x)  
> if x == “-f”  
> global folder\_path = ARGS[i+1]  
> end  
> end

Amazingly, I see that from inside the for loop I don’t see the variable **folder\_path** if i do not put “global” in front of the var ?!

Variable scoping in Julia are so drastic?  
I am thinking … oh … how many time i will code wrong to remember me this scoping rules!

I’m used to the fact that if I define a variable somewhere in the code, it will then be visible to the rest of the code …

Newlisp shocked me when I tried it, because the variables had a general scope even if declared inside the functions (!?) … here we are the opposite … I understand that the code is more secure and more separate … but it is mentally demanding that remind me that I don’t see external variables inside a for loop …

Panic … or I misunderstood?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 30, 2020, 10:13am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/2 "2020-01-30T10:13:33Z")

</div>

Hi there and welcome! How are you learning Julia? - this is covered in the manual quite well I think.

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 30, 2020, 10:26am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/3 "2020-01-30T10:26:42Z")

</div>

Hi Dario,

look, this is not so bad as you think. If you get used to write functions, this does not matter anymore:

```julia
function dario(folder_path="abc")
    for i in 1:3
        folder_path *= string(i)
        println("$i: $folder_path")
    end
end

julia> dario()
1: abc1
2: abc12
3: abc123

```

allora, no need to panic 🙂

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [January 30, 2020, 10:31am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/4 "2020-01-30T10:31:36Z")

</div>

In addition to the comments above this only applies to code executed in the global scope (e.g., from the REPL as you are doing). If you do this from within a function then it works as expected.

This was the subject of quite a lot of discussion and this behaviour has recently been change (for the REPL only) - see [RFC: bring back v0.6 scope rules in the REPL by JeffBezanson · Pull Request #33864 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/33864) which I presume will become v1.5 in due course.

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 10:57am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/5 "2020-01-30T10:57:16Z")

</div>

Damn! a function with my name makes a certain effect!

Thanks, il will use functions, the max i can.

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 11:04am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/6 "2020-01-30T11:04:34Z")

</div>

I have tried (also if nobody here is called foo 🙂 ) i this way:

```julia

> function pippo()
> x = 1
> for i in 1:10
> x = i
> end
> println("x=", x)
> end
> 
> y = 1
> for i in 1:10
> y = i
> end
> 
> println("y=", y)
> 
> pippo()

```

```julia
julia> 

y=1
x=10
julia>

```

Ok, so, in the function the scope is as i think it must be … ok, i will use functions, before to have this in the 1.5 release i think is better to enter this mentality. I think they will be right to change this behaviour because it is not it is not so homogeneous to have scoping different in REPL and in functiosn … but i think, there must have been valid reasons for doing so.  
Ok thanks.

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 11:34am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/7 "2020-01-30T11:34:03Z")

</div>

Yes, i have to read a manual, you are right … but, you know, when you test if a language is for you, and you do not have so much time, sometimes you go ahead here and there … avoiding a systematic reading that often takes a long time.

But now it’s time to read a manual … because … Julia has convinced me enough for now … so it’s time to start a sistematic reading …

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 30, 2020, 11:38am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/8 "2020-01-30T11:38:34Z")

</div>

😀 it’s partly that there are some out-of-date resources at large on the internet and it would be good to correct them if possible. (For example, I have to update the [Wikibook](https://en.wikibooks.org/wiki/Introducing_Julia) occasionally when people report problems.)

---

<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:** [January 30, 2020, 2:07pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/9 "2020-01-30T14:07:56Z")

</div>

> [@Darione](#):
>
> But now it’s time to read a manual … because … Julia has convinced me enough for now … so it’s time to start a sistematic reading …

No need to read end to end quite yet.

I would begin in Jupyter notebooks if you are just starting. It does not have the same issue you ran into (ie you wouldn’t have needed `global` there) and I think you will find it very intuitive there.

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 3:06pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/10 "2020-01-30T15:06:58Z")

</div>

Thanks @jperla.

Jupiter … mmm, yes I saw it in the videos … but I’m not a researcher … for now I’m using Juno with satisfaction … and then I’m interested in launching julia from the command line, to do scripting (scripting yes, but compiled, I mean. …) and some fast elaboration on texts. So, i have necessarily to focus on run Julia in command line …

How strange though … that is, it seems that inside a for loop, however, external objects are visible, while simple variables are not … if I do:

```julia
mutable struct Ttest
  value
end

t = Ttest(0)

b = 1
t.value = 1

for i in 1:10
  b = 2
  t.value = 2
end
println(b)
println(t.value)

```

i get this

```julia
julia> 
1
2
julia>

```

b is again 1, while the var declared as struct, has changed

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 3:07pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/11 "2020-01-30T15:07:53Z")

</div>

Thanks, i will get your pdf 🙂

---

<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:** [January 30, 2020, 3:13pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/12 "2020-01-30T15:13:41Z")

</div>

> [@Darione](#):
>
> Jupiter … mmm, yes I saw it in the videos … but I’m not a researcher

You don’t have to be a researcher at all. It is a nice interactive environment to start in, easy to install, and doesn’t have the confusing scoping gotchas that you hit.

Not absolutely necessary, but by the time you want to move back to juno these things will be very intuitive

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 3:31pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/13 "2020-01-30T15:31:56Z")

</div>

I think to have found my definitive solution: in the script inside.jl that i want to run from command line,  
i will do simply a “main()” function, and i will have no problems of scope!

```julia
function main()
  @show ARGS
  b = 4
  function change_inside()
    b = 5
  end
  change_inside()
  println("b inside is ", b)
end

main()

```

i get, running in command line:

```julia
c:\vmswap\juliaPrj\test>julia inside.jl 1 2 3
ARGS = ["1", "2", "3"]
b inside is 5

c:\vmswap\juliaPrj\test>

```

so no problem in scoping. 😃

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [January 30, 2020, 3:33pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/14 "2020-01-30T15:33:13Z")

</div>

However, i will install Jupiter, ok.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [January 30, 2020, 4:05pm UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/15 "2020-01-30T16:05:01Z")

</div>

have you tried [SoftGlobalScope](https://github.com/stevengj/SoftGlobalScope.jl)?

---

<div class="post-metadata">

**Author:** ![Darione](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/darione/32/12160_2.png) [@Darione](https://discourse.julialang.org/u/Darione)\
**Post date:** [February 3, 2020, 8:08am UTC](https://discourse.julialang.org/t/variabile-scope-into-the-for-panic/33971/16 "2020-02-03T08:08:19Z")

</div>

Thanks @Paul_Soderlind, i had a look the link you pass me … but perhaps i prefer to put myself into “more function” mentality, now that i have a (little) better understand on the Julia REPL behaviour. The important thing is to have understood clearly it, to not fall easily in bugs due to it. 🙂
