# Use of global variables

**URL:** https://discourse.julialang.org/t/use-of-global-variables/2194
**Category:** General Usage
**Created:** [February 21, 2017, 12:39am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194 "2017-02-21T00:39:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [February 21, 2017, 12:39am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/1 "2017-02-21T00:39:12Z")

</div>

Hi,  
I’m writing a Monte Carlo code in Julia that involves several functions that, in the end, have to deal with some variables that are transversal tomall the parts of the code, such as the density, temperature etc. My best bet is that these should be global variables, but if I understand that correctly, using global variables is a bad idea. The way I’m handling that is to define a user type that contains all these variables, as for instnance

type global  
dens:: Float64  
T::Float64  
global()=new()  
end

then declare a variable of this type, fill it with the right values, and pass it to all functions

glob = global()  
glob.dens = 0.02186  
glob.T=2.17

function u2(x::Float64,g::global)  
dens=g.dens  
return exp(-dens/dens)  
end

This seemsnto work fine and is performance-wise, but due to the use of the user data type, useful operations on whole arrays like

z=rand(1000)  
res=u2.(z,glob)

Give an error, stating it can not determine the size of glob.

What would then be the right way to handle that? Actually, what is the best way to deal with global variables in general? I know some will say that the best practice isnto avoid using them, but sometimes I don’t seem to be able to dind alternatives. And I want to avoid passing only the required variables in each case, as sometimes I may need to pass a lot of arguments…

Best regards and thanks,

Ferran.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [February 21, 2017, 12:45am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/2 "2017-02-21T00:45:38Z")

</div>

I guess I’m a little surprised that `global` can be a type name… Anyway.

It should be fixed on 0.6. The easiest workaround is to use `[glob]` instead.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [February 21, 2017, 12:46am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/3 "2017-02-21T00:46:46Z")

</div>

I guess it can’t. So nothing surprising…

```julia
julia> type global
ERROR: syntax: invalid type name "global"

```

---

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [February 21, 2017, 12:50am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/4 "2017-02-21T00:50:08Z")

</div>

Yes a bad name… Change it with something else like COMMON for instance. The question stilll holds 🙂  
Best,  
Ferran.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [February 21, 2017, 12:51am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/5 "2017-02-21T00:51:51Z")

</div>

Have you tried the workaround I give?

---

<div class="post-metadata">

### Author: ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)
#### Post date: [February 21, 2017, 6:57am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/6 "2017-02-21T06:57:09Z")

</div>

Ahh… No it was late night when I write. I’ll try this today, thanks 🙂  
Best,  
Ferran.

---

<div class="post-metadata">

### Author: ![mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mazzanti/32/32948_2.png) [@mazzanti](https://discourse.julialang.org/u/mazzanti)
#### Post date: [February 21, 2017, 8:36am UTC](https://discourse.julialang.org/t/use-of-global-variables/2194/7 "2017-02-21T08:36:54Z")

</div>

Yes that works nicely 🙂  
Thanks for your kind help,  
Ferran.
