# Global variables within 2 files

**URL:** <https://discourse.julialang.org/t/global-variables-within-2-files/40028>\
**Category:** New to Julia\
**Created:** [May 23, 2020, 3:54pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028 "2020-05-23T15:54:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![catosp](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@catosp](https://discourse.julialang.org/u/catosp)\
**Post date:** [May 23, 2020, 3:54pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/1 "2020-05-23T15:54:25Z")

</div>

Hello!

How can I declare a global variable that is shared between two files?

File1.jl

```julia
import("File2. jl") 

function1 test() 
    close=false
    a=0
    If a>0
        close = true
    end
end

```

File2.jl

```julia
function2 test2() 
    If close == true
        b=0
    end
end

```

The shared variable is close=true or false

Thank you!

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 23, 2020, 4:01pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/2 "2020-05-23T16:01:08Z")

</div>

There is nothing telling Julia that `close` is global. Defining `close` in at the global level before importing File2.jl should ensure that Julia knows where close is defined.

File1.jl

```julia
close = true

import("File2.jl")

function1 test()
    close=false
    a=0
    if a>0
        close = true
    end
end

```

File2.jl

```julia
function2 test2()
    if close == true
        b=0
    end
end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 23, 2020, 6:17pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/3 "2020-05-23T18:17:25Z")

</div>

Please quote your code when you ask a question: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

It is a very bad idea to (i) use global variables, and (ii) especially a bad idea to share them between different files!

Instead, pass the variables as arguments to your functions. If you have many parameters, wrap them up into a type, e.g. using Parameters.jl.

---

<div class="post-metadata">

**Author:** ![catosp](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@catosp](https://discourse.julialang.org/u/catosp)\
**Post date:** [May 24, 2020, 6:56am UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/4 "2020-05-24T06:56:45Z")

</div>

@dpsanders i think is imposible to pass the variable as arguments to functions when function1 from File1.jl and function2 from File2.jl is running on different threads continuously. The condition for function2 to stop is given by the function1. Ex: the function1 will continously readline() and if I write in console the string “stop” then the function2 will stop, if not the function2 will run continuously.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 24, 2020, 7:31am UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/5 "2020-05-24T07:31:40Z")

</div>

Ah well you didn’t mention threads in your original question.

That sounds like (I believe) a use-case for `Channel`s:

[https://docs.julialang.org/en/v1/manual/parallel-computing/#Channels-1](https://docs.julialang.org/en/v1/manual/parallel-computing/#Channels-1)

---

<div class="post-metadata">

**Author:** ![catosp](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@catosp](https://discourse.julialang.org/u/catosp)\
**Post date:** [May 24, 2020, 11:24am UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/6 "2020-05-24T11:24:26Z")

</div>

@pixel27 I tried your method and didn’t work.

File1.jl

```julia
import Base.Threads.@spawn

include("File2.jl")

close = false

function test()
	while true
		word = readline()
		if word == "close"
			close = true
			println("you choose to close")
		end
	end
end

t1 = @spawn test()
t2 = @spawn runF()
fetch(t1)
fetch(t2)

```

File2.jl

```julia
function runF()
	for i in 1:50
		println(close)
		sleep(5)
		if close == true
			println("closed")
			break
		end
	end
end

```

when run the File1.jl the results is  
false  
false  
false  
…

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [May 24, 2020, 11:45am UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/7 "2020-05-24T11:45:41Z")

</div>

> [@catosp](#):
>
> @pixel27 I tried your method and didn’t work.

You defined `close` **after** you imported “File2.jl”. When defining functions Julia compiles the function before executing it. So if you did this:

```julia
function parent()
    function child(a)
         b = a
    end

    b = "abc"
   
    child("xyz")

    println(b)
end

```

When it’s compiled Julia will notice that “b” was defined in the parent scope so the `child()` function will know to set “b” in the parent scope, rather than “b” in the child scope. So with this example it will print “xyz”.

When executing top level commands in a file, Julia **does not** compile the whole file then execute it, it runs like a script, i.e. one line at a time. Which means at the time it included “File2.jl” there was no definition for “close” yet, that line has not been processed yet. Which means this:

```julia
function child(a)
     b = a
end

b = "abc"
   
child("xyz")

println(b)

```

Will actually print “abc” even though it’s the SAME code that was in the parent function above. This is one of the little gotchas with Julia. 🙂

---

<div class="post-metadata">

**Author:** ![catosp](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@catosp](https://discourse.julialang.org/u/catosp)\
**Post date:** [May 24, 2020, 12:04pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/8 "2020-05-24T12:04:03Z")

</div>

It works now ! Thank you all !

In the File1.jl you need to specify that variable that is changed is global

```julia
if word == "close"
			global close = true
			println("you choose to close")
			println(close)
		end

```

So you need to specify “global close = true” instead of “close = true”

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [May 24, 2020, 1:29pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/9 "2020-05-24T13:29:24Z")

</div>

Maybe one thing to keep in mind is that files don’t really have semantic significance in Julia. Splitting things into two files is fine but it’s just a matter of organization; if you do

```julia
A = ...

include(“FileB.jl”)

...

```

then it’s just the same as if you copy and paste the contents of “FileB.jl” into where the `include` statement is.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 24, 2020, 4:24pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/10 "2020-05-24T16:24:47Z")

</div>

Do you mean all `global` variables or `global`s in `Main`? Because there are many good uses for a specific module `global` variable. And if the module is very big, I often break it into many files that make reference to the same module `global` variable.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 24, 2020, 4:35pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/11 "2020-05-24T16:35:10Z")

</div>

The general advice in software engineering is to avoid global variables whenever possible, since it makes understanding the code much harder.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [May 24, 2020, 6:30pm UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/12 "2020-05-24T18:30:19Z")

</div>

This, and the performance penalty of global variables in Julia (as long as [Type annotations in global scope · Issue #964 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/964) is not implemented).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 25, 2020, 1:14am UTC](https://discourse.julialang.org/t/global-variables-within-2-files/40028/13 "2020-05-25T01:14:49Z")

</div>

They do not have a performance penalty if you fix the binding by using `const`. And by using `const GLOBAL = RefValue{TYPE_T}(OBJECT_OF_TYPE_T)` you have a global of any type you want that you can change the value and has no performance problems.
