# Global variables

**URL:** https://discourse.julialang.org/t/global-variables/70347
**Category:** General Usage
**Created:** [October 25, 2021, 5:12pm UTC](https://discourse.julialang.org/t/global-variables/70347 "2021-10-25T17:12:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![dmgoldschmidt](https://avatars.discourse-cdn.com/v4/letter/d/82dd89/32.png) [@dmgoldschmidt](https://discourse.julialang.org/u/dmgoldschmidt)
#### Post date: [October 25, 2021, 5:12pm UTC](https://discourse.julialang.org/t/global-variables/70347/1 "2021-10-25T17:12:21Z")

</div>

The inaccessibility inside loops of variables defined in the global scope is driving me nuts. Do I really have to use the keyword global every time I refer to one in a “local” scope? I’d like to have a statement in global scope (or even better, a condition attached to the global variable) which says that it can be accessed everywhere, not just in the global scope. Is there a way to do this

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [October 25, 2021, 5:18pm UTC](https://discourse.julialang.org/t/global-variables/70347/2 "2021-10-25T17:18:17Z")

</div>

Typically, you should avoid using global variables, but it should not require the global keyword in most cases. For example, the following runs in Julia 1.6.3:

```julia
x = 1
for i in 1:10
    println("x = $x")
end

```

Is there a specific case where it is not working as expected?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 25, 2021, 5:20pm UTC](https://discourse.julialang.org/t/global-variables/70347/3 "2021-10-25T17:20:59Z")

</div>

> [@dmgoldschmidt](#):
>
> Do I really have to use the keyword global every time I refer to one in a “local” scope?

If you’re not using functions - yes. This is by design, as anything can happen at any time in global scope, due to julia’s parallel dynamic nature and `eval`. Passing arguments around explicitly makes it much easier for the compiler to optimize your code, as it doesn’t have to insert lookups to global variables at every access.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 25, 2021, 5:29pm UTC](https://discourse.julialang.org/t/global-variables/70347/4 "2021-10-25T17:29:23Z")

</div>

Some suggestions in this thread: [Best solution to Julia's soft scope problem? - #5 by tim.holy](https://discourse.julialang.org/t/best-solution-to-julias-soft-scope-problem/70199/5)

(probably there should be a warning at the very start of the Julia manual telling people to never, ever, use the global keyword, until you know the language enough to fully understand that it is unavoidably needed. The best advice is to never use it, there is almost always a better way to do things without it).

---

<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: [October 25, 2021, 6:35pm UTC](https://discourse.julialang.org/t/global-variables/70347/5 "2021-10-25T18:35:42Z")

</div>

> [@dmgoldschmidt](#):
>
> The inaccessibility inside loops of variables defined in the global scope is driving me nuts.

I understand completely. A few changes in your workflow will mean you never hit this problem again though.

Just remeber that:

1. Softscope (i.e., what you find intuitive) applies to the REPL
2. Softscope applies to Jupyter notebooks
3. Softscope applies to VSCode when a `.jl` file is being executed inline (and I think debugged)
4. The behavior of code inside of functions is completely consisetnt with softscope
5. If you run things as a `.jl` file with an `include` etc. then you run into the thing you find confusing.

So note that this only happens in scripts that are called (and not executed in vscode inline). Everything else will be intuitive.

The solution to the problem at this point is:

1. Feel free to have loops outside of functions in jupyter notebooks.
2. Never write loops in `.jl` files that are not wrapped in a function. You would want to do this anyways for performance reasons. This will also ensure that things are consistent if you use the inline VS Code vs. the `F5` to exdecute them.
3. If you ever use the `global` keyword and it wasn’t an intention design, then it probably means something is wrong. You probably should be wrapping something in a function There are almost no cases where that keyword is necessary for normal usage.

---

<div class="post-metadata">

### Author: ![dmgoldschmidt](https://avatars.discourse-cdn.com/v4/letter/d/82dd89/32.png) [@dmgoldschmidt](https://discourse.julialang.org/u/dmgoldschmidt)
#### Post date: [October 25, 2021, 6:45pm UTC](https://discourse.julialang.org/t/global-variables/70347/6 "2021-10-25T18:45:08Z")

</div>

When I run this:

mutable struct Process  
parent::Int64  
generation::Int64  
has\_childen::Bool  
start\_time::Float64  
end

function main()  
process = Process(0,1,false,0)  
println(fieldnames(Process))  
process.has\_children = true  
end  
main()

I get this:

%julia julia\_bug.jl  
Process(0, 1, false, 0.0)  
(:parent, :generation, :has\_childen, :start\_time)  
ERROR: LoadError: type Process has no field has\_children  
Stacktrace:  
[1] setproperty!(x::Process, f::Symbol, v::Bool)  
@ Base ./Base.jl:34  
[2] main()  
@ Main ~/code/hawkes/julia\_bug.jl:12  
[3] top-level scope  
@ ~/code/hawkes/julia\_bug.jl:14  
in expression starting at /home/david/code/hawkes/julia\_bug.jl:14

Looks like a bug. What am I missing?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [October 25, 2021, 6:48pm UTC](https://discourse.julialang.org/t/global-variables/70347/7 "2021-10-25T18:48:44Z")

</div>

You have a typo, in the definition of `Process`, you write `childen` instead of `children`.

---

<div class="post-metadata">

### Author: ![dmgoldschmidt](https://avatars.discourse-cdn.com/v4/letter/d/82dd89/32.png) [@dmgoldschmidt](https://discourse.julialang.org/u/dmgoldschmidt)
#### Post date: [October 25, 2021, 6:52pm UTC](https://discourse.julialang.org/t/global-variables/70347/8 "2021-10-25T18:52:28Z")

</div>

Aargh! Thanks for reading carefully. Why can’t I do that myself???

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [October 25, 2021, 6:56pm UTC](https://discourse.julialang.org/t/global-variables/70347/9 "2021-10-25T18:56:32Z")

</div>

But that doesn’t seem to have nothing to do with the global scope, nor was it fixed by using the `global` keyword. Did you post in a mistaken post?

---

<div class="post-metadata">

### Author: ![dmgoldschmidt](https://avatars.discourse-cdn.com/v4/letter/d/82dd89/32.png) [@dmgoldschmidt](https://discourse.julialang.org/u/dmgoldschmidt)
#### Post date: [October 25, 2021, 7:09pm UTC](https://discourse.julialang.org/t/global-variables/70347/10 "2021-10-25T19:09:32Z")

</div>

It’s a double blunder. I first thought it was a scoping problem, and hurriedly posted that mistake (although I’ve run across the issue on a previous julia project – the “softscope” issue is one I haven’t encountered before because I’ve been a C++ programmer for years). Of course I could have saved everyone everyone a lot of time by just reading my code carefully. So mea culpa, and thanks so much for your quick responses!
