# How to correctly define and use global variables in the module in Julia?

**URL:** https://discourse.julialang.org/t/how-to-correctly-define-and-use-global-variables-in-the-module-in-julia/65720
**Category:** General Usage
**Created:** [August 3, 2021, 5:46am UTC](https://discourse.julialang.org/t/how-to-correctly-define-and-use-global-variables-in-the-module-in-julia/65720 "2021-08-03T05:46:29Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 4, 2021, 4:04am UTC](https://discourse.julialang.org/t/how-to-correctly-define-and-use-global-variables-in-the-module-in-julia/65720/8 "2021-08-04T04:04:46Z")

</div>

I also say that you do not need explicit `global` declaration here at all. (See below.)

**A.** The `global` and the two `::Float64`’s below are redundant:

```julia
module A
global a = 100.0::Float64
function f(x)::Float64
    a = x^2
    return a
end
println("f(6.0) = ", f(6.0), ", a = ", a)
end;

```

```julia
f(6.0) = 36.0, a = 100.0

```

**B.** The A above is equivalent to the following:

```julia
module B
a = 100.0
function f(x)::Float64
    a = x^2
    return a
end
println("f(6.0) = ", f(6.0), ", a = ", a)
end;

```

```julia
f(6.0) = 36.0, a = 100.0

```

**B′.** Don’t write return-type in general (cf. [return-type](https://github.com/JuliaLang/julia/blob/master/doc/src/manual/functions.md#return-type)). B′ is better than B and B is better than A.

```julia
module B′
a = 100.0
function f(x)
    a = x^2
    return a
end
println("f(6.0) = ", f(6.0), ", a = ", a)
end;

```

```julia
f(6.0) = 36.0, a = 100.0

```

**C.** We need `global` for the _immutable_ global variable `a` in function.

```julia
module C
a = 100.0
function f(x)
    global a = x^2
    return a
end
println("f(6.0) = ", f(6.0), ", a = ", a)
end;

```

```julia
f(6.0) = 36.0, a = 36.0

```

**D.** We don’t need `global` for the _content_ `a[]` of the _mutable_ global variable `a`:

```julia
module D
a = Ref(100.0)
function f(x)
    a[] = x^2
    return a[]
end
println("f(6.0) = ", f(6.0), ", a[] = ", a[])
end;

```

```julia
f(6.0) = 36.0, a[] = 36.0

```

**E.** The D above is type-unstable. We need `const` for type stability:

```julia
module E
const a = Ref(100.0)
function f(x)
    a[] = x^2
    return a[]
end
println("f(6.0) = ", f(6.0), ", a[] = ", a[])
end;

```

```julia
f(6.0) = 36.0, a[] = 36.0

```

For type (un-)stability, please compare the results of `@code_warntype D.f(6.0)` and `@code_warntype E.f(6.0)`.

**Postscript:** A code will be more readable and efficient if the global variables that store the parameters of a problem are passed as arguments to functions _everytime_. See [**Problem-Algorithm-Solver pattern**](https://discourse.julialang.org/t/function-depending-on-the-global-variable-inside-module/64322/10).

---

_[View the full topic](https://discourse.julialang.org/t/how-to-correctly-define-and-use-global-variables-in-the-module-in-julia/65720)._
