# Why it is possible to use a global variable in a custom function that declares later than the function declaration?

**URL:** <https://discourse.julialang.org/t/why-it-is-possible-to-use-a-global-variable-in-a-custom-function-that-declares-later-than-the-function-declaration/114034>\
**Category:** New to Julia\
**Tags:** question, namespaces\
**Created:** [May 9, 2024, 4:14am UTC](https://discourse.julialang.org/t/why-it-is-possible-to-use-a-global-variable-in-a-custom-function-that-declares-later-than-the-function-declaration/114034 "2024-05-09T04:14:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Monkiesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monkiesh/32/209108_2.png) [@Monkiesh](https://discourse.julialang.org/u/Monkiesh)\
**Post date:** [May 9, 2024, 4:14am UTC](https://discourse.julialang.org/t/why-it-is-possible-to-use-a-global-variable-in-a-custom-function-that-declares-later-than-the-function-declaration/114034/1 "2024-05-09T04:14:12Z")

</div>

Hi Julianners, I am new to Julia.  
Recently, I find it’s weird that I can use a global variable declares later when I am writing a function. Here is an example:

```julia
function p(a::Int)
    return a+b
end

b = 2
c = p(1)
@show c

```

It is quite different to what it is in C/C++ or Python. And makes me really confused.  
Is that because the compiler ignores the function until it is being used, or some other reasons?  
Is that for the same reason we can write _export_ at the beginning of a module, before everything is declared.  
Is that common or discouraged to use a global variable declares later while writing my functions? (so that I do not need to pass them as parameters)

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [May 9, 2024, 4:37am UTC](https://discourse.julialang.org/t/why-it-is-possible-to-use-a-global-variable-in-a-custom-function-that-declares-later-than-the-function-declaration/114034/2 "2024-05-09T04:37:13Z")

</div>

Global variables, in general, are discouraged (and have been in pretty much all programming for decades now).

In Julia, when you define your function `p` the compiler simply notes that when executed you want to look up whatever the value of `b` is at that time. You can see this by using `@code_lowered`:

```julia
@code_lowered p(1)
CodeInfo(
1 ─ %1 = a + Main.b
└── return %1
)

```

It’s not necessary to declare variables ahead of time - in fact you don’t need to declare functions or anything else. For example:

```julia
f(a) = g(a)
g(a) = a + 1

```

This works fine as the compiler is happy to accept that when `f` is called it is to use whatever definition of `g` that it has at the time.

This is important for multiple dispatch. The precise method to call will depend on the types of the actual arguments - and this may not be known at time of declaration.

But please, reconsider the usage of global variables. There are many alternatives depending on the exact use-case.

---

<div class="post-metadata">

**Author:** ![Monkiesh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/monkiesh/32/209108_2.png) [@Monkiesh](https://discourse.julialang.org/u/Monkiesh)\
**Post date:** [May 9, 2024, 5:23am UTC](https://discourse.julialang.org/t/why-it-is-possible-to-use-a-global-variable-in-a-custom-function-that-declares-later-than-the-function-declaration/114034/3 "2024-05-09T05:23:52Z")

</div>

thanks a lot for your helpful reply. 🥰

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [May 9, 2024, 8:14am UTC](https://discourse.julialang.org/t/why-it-is-possible-to-use-a-global-variable-in-a-custom-function-that-declares-later-than-the-function-declaration/114034/4 "2024-05-09T08:14:30Z")

</div>

Just curious, but how is that different from Python?  
For me, the following

```python
def p(a):
  return a + b

b = 2
c = p(1)
print(c)

```

works just the same.

PS: Also some compiled languages allow arbitrary forward declarations, e.g., in Haskell even this works:

```haskell
main = putStrLn $ show c

p(a) = a + b

c = p(1)
b = 2

```

In the end, it’s more a restriction of the compiler than anything else.
