# Global variable issue

**URL:** <https://discourse.julialang.org/t/global-variable-issue/108288>\
**Category:** General Usage\
**Tags:** question, scope, while-for-scope\
**Created:** [January 3, 2024, 9:11am UTC](https://discourse.julialang.org/t/global-variable-issue/108288 "2024-01-03T09:11:29Z")\
**Posts on this page:** 1\
**Showing post:** 15

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [January 3, 2024, 3:22pm UTC](https://discourse.julialang.org/t/global-variable-issue/108288/15 "2024-01-03T15:22:05Z")

</div>

`basis`, `s`, `N`, and everything else defined outside the loop are global variables, meaning they are defined at the topmost level. The line `s=s+1` is inside the local scope of the `while` loop. Here Julia is not sure if you want to use the global `s` or define a new local `s` variable, so it throws an error. There are `local` and `global` keywords that can force one behavior or the other.

This issue is tricky to work around at first, especially with `while` loops when testing and learning the language (with scripts), but it will go away once you start using Julia more like how it was designed to be used (with functions that accept global variables as arguments). I went through the same transition and had many of the same struggles you are facing. @mkitti’s answer has the common workarounds. You can use `let` or `global` as quick fixes for your scripts, but the long-term solution is to starting thinking in terms of functions. If you use functions, then you don’t need to bother learning the complicated hard/soft scoping rules; the loops will work as you expect.

Some links:

> [@Memory Pre-allocation in the global scope](https://discourse.julialang.org/t/memory-pre-allocation-in-the-global-scope/85861/19):
>
> Let me try to summarize (because there is a lot of confusion around this): Your initial approach is fine in terms of performance. “Avoid global variables” means avoid inheriting global variables through scoping. Your code will use global variables as inputs and outputs; this is normal. F1!() could have been defined without taking any arguments and still work. Then it would have to obtain the values from the outer (in this case global) scope. That would be bad practice and slow unless those …

> [@Why does the soft scope warning not appear in a nested loop?](https://discourse.julialang.org/t/why-does-the-soft-scope-warning-not-appear-in-a-nested-loop/72646/8):
>
> You can always declare a variable as local to avoid it picking up any previous definitions: function f() i = 100 for j in 1:2 local i = 0 i = i + j end println(i) end julia\> f() 100

> [@Why and when to pass variables to sub-functions?](https://discourse.julialang.org/t/why-and-when-to-pass-variables-to-sub-functions/49073):
>
> I am working on spitting my functions up into many smaller sub-functions. The issue I came across is that I need to pass more and more variables to successive sub-functions. How is this typically handled? I see three possibilities: Pass all the needed variables to every sub-function. Don’t pass any variables to sub-functions and rely on inheritance from outer scope. Re-compute quick one-liners like array sizes inside every sub-function and then only pass the arrays themselves. Option 1 seems…

---

_[View the full topic](https://discourse.julialang.org/t/global-variable-issue/108288)._
