# Unexpected scope issue

**URL:** https://discourse.julialang.org/t/unexpected-scope-issue/30145
**Category:** General Usage
**Tags:** question
**Created:** [October 21, 2019, 6:54pm UTC](https://discourse.julialang.org/t/unexpected-scope-issue/30145 "2019-10-21T18:54:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![marcusps](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcusps/32/537_2.png) [@marcusps](https://discourse.julialang.org/u/marcusps)
#### Post date: [October 21, 2019, 6:54pm UTC](https://discourse.julialang.org/t/unexpected-scope-issue/30145/1 "2019-10-21T18:54:40Z")

</div>

If I try to run the following

```julia
a = 1:10
while length(a)>0
  [0 for i in a]
  a = first(a)+1:last(a)
end

```

I get an `UndefVarError: a not defined` error.

If I add a `global a` like so

```julia
a = 1:10
while length(a)>0
  global a
  [0 for i in a]
  a = first(a)+1:last(a)
end

```

everything runs as expected. I get this in `for` loops as well (the code is nonsense, but a pretty minimal example).

Is this the intended behavior? This is Julia 1.0.5 on Windows.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [October 21, 2019, 7:05pm UTC](https://discourse.julialang.org/t/unexpected-scope-issue/30145/2 "2019-10-21T19:05:33Z")

</div>

Yes, it is intended (in global scope).

Inside a function, the behavior is different. E.g., this works:

```julia
function foo()

a = 1:10
while length(a)>0
  [0 for i in a]
  a = first(a)+1:last(a)
end

end

```

See the [docs on variable scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

---

<div class="post-metadata">

### Author: ![marcusps](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcusps/32/537_2.png) [@marcusps](https://discourse.julialang.org/u/marcusps)
#### Post date: [October 21, 2019, 8:20pm UTC](https://discourse.julialang.org/t/unexpected-scope-issue/30145/3 "2019-10-21T20:20:29Z")

</div>

Thanks!
