# A question about variable scope

**URL:** https://discourse.julialang.org/t/a-question-about-variable-scope/27335
**Category:** General Usage
**Tags:** question
**Created:** [August 8, 2019, 9:15pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335 "2019-08-08T21:15:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [August 8, 2019, 9:15pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/1 "2019-08-08T21:15:40Z")

</div>

I met a problem that make me confused. It is about the scope of variable in Julia.

```julia
x = 1
while x <= 10
      x=x+1
      println(x)
end

```

In this case, julia shows “UndefVarError: x not defined”. Can anyone explain to me why this will happen?

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [August 8, 2019, 9:18pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/2 "2019-08-08T21:18:56Z")

</div>

Also, I find that if I change this code like this

```julia
x = 1
while x <=10
      global x = x+1
      println(x)
end

```

This code works but I really don’t want to add a “global” in my code.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [August 8, 2019, 9:24pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/3 "2019-08-08T21:24:30Z")

</div>

This is the (famous) global scope “issue”. You should find a lot about it here on discourse.

Put your code in a function and it works as you want.

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [August 8, 2019, 9:30pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/4 "2019-08-08T21:30:24Z")

</div>

Thank you! btw, it this due to Julia language itself? If I update to the latest version, will this global scope “issue” be solve?

---

<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: [August 8, 2019, 9:47pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/5 "2019-08-08T21:47:06Z")

</div>

No. However, if you use IJulia (Jupyter notebooks), you won’t need to use global. This is because of a settings change for IJulia.

---

<div class="post-metadata">

### Author: ![wenqi\_kou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wenqi_kou/32/9348_2.png) [@wenqi\_kou](https://discourse.julialang.org/u/wenqi_kou)
#### Post date: [August 8, 2019, 9:49pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/6 "2019-08-08T21:49:00Z")

</div>

thank you!

---

<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: [August 8, 2019, 10:32pm UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/7 "2019-08-08T22:32:30Z")

</div>

You can also access this behavior outside of IJulia with the [SoftGlobalScope.jl](https://github.com/stevengj/SoftGlobalScope.jl) package.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [August 9, 2019, 1:02am UTC](https://discourse.julialang.org/t/a-question-about-variable-scope/27335/8 "2019-08-09T01:02:03Z")

</div>

You can, but you probably shouldn’t 😉
