# Globals / Constants Act Differently Depending on Use?

**URL:** https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933
**Category:** Performance
**Tags:** constants, typed-globals
**Created:** [February 2, 2023, 3:56pm UTC](https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933 "2023-02-02T15:56:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dkzwart](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@dkzwart](https://discourse.julialang.org/u/dkzwart)
#### Post date: [February 2, 2023, 3:56pm UTC](https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933/1 "2023-02-02T15:56:19Z")

</div>

I’ve been doing a lot of reading on Constants and Globals, and I realize I really don’t understand what I am reading. Having said that, can you all help me understand why var1 is undefined in the function? It seems to be because I update the value of the Global constant in the function?

Function:

```julia
function test()
     println(@isdefined var1)
     println(@isdefined var2)
     if var1
          var1 = false
     elseif !var1
          var1 = true
     end
end

```

Calling code:

```julia
using Revise
includet("test.jl")

global var1 = var2 = true

test()

```

Output:

```julia
julia> test()
false
true
ERROR: UndefVarError: var1 not defined
Stacktrace:
 [1] test()
   @ Main c:\Users\dkzwa\Desktop\figs\test.jl:4
 [2] top-level scope
   @ REPL[5]:1

```

Is this the only way to overcome this obstacle? It works, but I don’t understand what is going on here:

> [@How to correctly define and use global variables in the module in Julia?](https://discourse.julialang.org/t/how-to-correctly-define-and-use-global-variables-in-the-module-in-julia/65720/2):
>
> Hi CRquantum! And welcome to the Julia discourse! [Having global variables is not considered the best practice](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables) because it makes code difficult to track. The other disadvantage is that unless it has a constant type it will affect your performance. But this is what I think you are trying to solve. If you still want to do this, you can use [Ref](https://docs.julialang.org/en/v1/base/c/#Core.Ref): global const var = Ref{Float64}(0.0) # Store it into a `Ref` which it's mutable but has a constant type function f(x::Float64) global var[] = 2.0\*x …

After further digging, I find that adding one global to var1 in the function makes the function see all var1 in the function as the global var1. The following function works.

```julia
function test()
     println(@isdefined var1)
     println(@isdefined var2)
     if var1
          global var1 = false
     elseif !var1
          var1 = true
     end 
end

```

With the output alternating between:

```julia
julia> test()
true
true
false

```

and

```julia
julia> test()
true
true
true

```

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [February 2, 2023, 4:39pm UTC](https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933/2 "2023-02-02T16:39:03Z")

</div>

A function defines a _hard local scope_. This is detailed in the manual [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope)

In particular, the following passage describes how assigned variables are handled:

> When x = occurs in a local scope, Julia applies the following rules to decide what the expression means based on where the assignment expression occurs and what x already refers to at that location:

> Existing local: If x is already a local variable, then the existing local x is assigned;  
> Hard scope: If x is not already a local variable and assignment occurs inside of any hard scope construct (i.e. within a let block, function or macro body, comprehension, or generator), a new local named x is created in the scope of the assignment;

That means the global variable var1 is shadowed by the local one you assign in the function.  
To circumvent this, variables can be annotated with global inside a function as you did subsequently.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [February 2, 2023, 5:38pm UTC](https://discourse.julialang.org/t/globals-constants-act-differently-depending-on-use/93933/3 "2023-02-02T17:38:45Z")

</div>

Your `test` function only has a single local scope and in a single scope a variable can only have one meaning. By writing `global var1` you are saying “In this scope, `var1` means the global variable `var1`.” You can have a different `var1` that shadow that if you introduce a new inner scope.
