# Scope of the variable

**URL:** <https://discourse.julialang.org/t/scope-of-the-variable/20996>\
**Category:** New to Julia\
**Created:** [February 19, 2019, 11:11pm UTC](https://discourse.julialang.org/t/scope-of-the-variable/20996 "2019-02-19T23:11:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj\_Pawar](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@Suraj\_Pawar](https://discourse.julialang.org/u/Suraj_Pawar)\
**Post date:** [February 19, 2019, 11:11pm UTC](https://discourse.julialang.org/t/scope-of-the-variable/20996/1 "2019-02-19T23:11:18Z")

</div>

I have a main function as below:  
include(“hello.jl”)  
import .hello  
rms = 0.0  
hello.foo(rms)  
println(rms)  
and hello function is as below:  
module hello  
function foo(rms)  
rms = 1.0  
println(rms)  
end  
end  
If i run the main.jl I get the output as 1.0 and 0.0. So the variable rms does not get changed by the function hello. I think it is got to do with the scope of the variable. Is there a way I can modify the variable inside the function. I can modify an array but not the variable inside the function.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [February 20, 2019, 12:45am UTC](https://discourse.julialang.org/t/scope-of-the-variable/20996/2 "2019-02-20T00:45:35Z")

</div>

Variables defined within a function are local to that function, and, usually, they are regenerated with each call to the function. When people want to have a value that is alterable from one call to another, the common practice is to use an argument to that function to pass the current value and keep the changing value in a variable outside of the called function. Is there a reason that is not suitable for your purpose?

Alternatively, you can make the variable global to the module.

```julia
module Example
    export example

    var = 0.0

    function example(increment)
         global var
         var += increment
         return var
    end
end

using .Example

example(1.0) # shows 1.0
example(1.0) # shows 2.0

```

---

<div class="post-metadata">

**Author:** ![Gyoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyoshi/32/7119_2.png) [@Gyoshi](https://discourse.julialang.org/u/Gyoshi)\
**Post date:** [February 20, 2019, 1:29am UTC](https://discourse.julialang.org/t/scope-of-the-variable/20996/3 "2019-02-20T01:29:37Z")

</div>

Surrounding your code blocks in ``` makes them easier to read

main.jl

```julia
include(“hello.jl”)
import .hello
rms = 0.0
hello.foo(rms)
println(rms)

```

hello.jl

```julia
module hello
function foo(rms)
rms = 1.0
println(rms)
end
end

```

---

<div class="post-metadata">

**Author:** ![Gyoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyoshi/32/7119_2.png) [@Gyoshi](https://discourse.julialang.org/u/Gyoshi)\
**Post date:** [February 20, 2019, 1:29am UTC](https://discourse.julialang.org/t/scope-of-the-variable/20996/4 "2019-02-20T01:29:38Z")

</div>

See this section of the the FAQ:  
[https://docs.julialang.org/en/v1.0/manual/faq/#I-passed-an-argument-x-to-a-function,-modified-it-inside-that-function,-but-on-the-outside,-the-variable-x-is-still-unchanged.-Why?-1](https://docs.julialang.org/en/v1.0/manual/faq/#I-passed-an-argument-x-to-a-function,-modified-it-inside-that-function,-but-on-the-outside,-the-variable-x-is-still-unchanged.-Why?-1)

By the way, functions that change their arguments by Julia convention are appended by an exclamation mark, e.g. `sort!(foo)`.  
I recommend doing the tutorials on [Juliabox.com](http://Juliabox.com)
