# Are the scope rules applied correctly by Julia?

**URL:** <https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 29, 2021, 5:26pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175 "2021-03-29T17:26:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 5:26pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/1 "2021-03-29T17:26:06Z")

</div>

Hello guys! In test1, array A has a local scope, shouldn’t it be global? In test2, array A has a global scope (OK). That seems wrong to me. Both scopes must be global. The difference in the two situations is very subtle! Please help me to understand correctly.

```julia
A = [1,2,3]

function test1()
	println("test1")
	A = [4,5,6] # can't take global ref
end

test1()
println(A) # = [1, 2, 3]

function test2()
	println("test2")
	A[1:end] = [4,5,6] # catch global ref, global A = [4,5,6] # It also works
end

test2()
println(A) # = [4, 5, 6]

```

Julia Version 1.6.0  
Commit f9720dc2eb (2021-03-24 12:55 UTC)  
Platform Info:  
OS: Linux (x86\_64-pc-linux-gnu)  
CPU: Intel(R) Core™ i3-7020U CPU @ 2.30GHz  
WORD\_SIZE: 64  
LIBM: libopenlibm  
LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [March 29, 2021, 5:33pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/2 "2021-03-29T17:33:32Z")

</div>

Hi. The rules are applied correctly: In the first example you _assign_ a new object to the variable `A` the first time it is seen, and that makes it a local variable. In the second example you _modify_ the variable `A` the first time it is seen; that’s partly a “read” operation, because you access its contents to select what part of the array must be replaced, so it is assumed to be a global.

---

<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:** [March 29, 2021, 5:40pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/3 "2021-03-29T17:40:18Z")

</div>

Note that this behaviour is exactly the same as e.g. in Python.

---

<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:** [March 29, 2021, 5:40pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/4 "2021-03-29T17:40:29Z")

</div>

I really recommend reading [Manual \> Scope of Variables \> Local Scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope), or even the whole _Scope of variables_ section. Also, note there may be small differences on how scope behave based on if you are executing the code in the REPL or from a file.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 5:50pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/5 "2021-03-29T17:50:12Z")

</div>

So is it always good to use global A, B, … within functions? For example:

```julia
A = [1,2,3]
function test()
	global A
	println("test1")
	A = [4,5,6]
end
test()
println(A) # [4,5,6]

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 5:53pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/7 "2021-03-29T17:53:43Z")

</div>

> [@marllos](#):
>
> So is it always good to use global A, B, … within functions?

No! rs…

If you want to use a global variable, you have to do that. But you should not do that almost never, because that hurts performance a lot. See the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/), the first one!

You should pass the variables as arguments to your function, i. e.

```julia
A = [1,2,3]
function test(A)
  println(A)
end

julia> test(A)
[1, 2, 3]

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [March 29, 2021, 6:03pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/8 "2021-03-29T18:03:12Z")

</div>

In addition to the advice given by @lmiq, if what you want is to update the values of `A`, while keeping the type of its elements and also its size, you can proceed as in your second example, but a nicer way of doing it – without indexing – is:

```julia
A = [1,2,3]

function test()
	A .= [4,5,6] # note that it is a "dotted assignment"
end

```

The “dotted assignment” means “replace the elements of `A` by the elements of the right hand side”, in contrast to `=` that means “replace the whole object referred to by `A` by the object on the right hand side”. That doesn’t have the downside of modifying globals mentioned before.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 29, 2021, 6:05pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/9 "2021-03-29T18:05:31Z")

</div>

> [@lmiq](#):
>
> But you should not do that almost never, because that hurts performance a lot.

And not just that. Global variables are _bad_ for all sorts of reasons, and you should almost never use them.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 6:17pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/10 "2021-03-29T18:17:57Z")

</div>

Global variables are good essentially to represent effectively constant values, like the speed of light. In which case declare them as _constant_ globals:

```julia
const c = 299792458

```

This you can use inside a function without performance problems, but you cannot modify it\* (you can, but that is another story, normally you should not and the standard behaviour and errors reflects that).

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 6:26pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/11 "2021-03-29T18:26:58Z")

</div>

And using constant in a global scope is right?

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 6:30pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/12 "2021-03-29T18:30:44Z")

</div>

I liked the “dotted assignment” !

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 6:31pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/13 "2021-03-29T18:31:40Z")

</div>

> [@marllos](#):
>
> And using constant in a global scope is right?

“right” may mean many things. What I meant is that you can use a global constant variable in good code, and that will perform well and also be easy to understand and predictable.

One other reason for not using non-constant global variables is that their values can change and so the result of a function can be unpredictable. For example:

```julia
function f(x)
   return x + a
end

```

what does this function return? That depends on what `a` is. If `a` is a trully constant value defined in your code, like the speed of light, declared as `const a = ...`, then someone will be able to predict the result of the function (and the compiler will know everything that it need about the function to optimize it). If `a` is not a constant, it may change depending on the definition of `a` provided at runtime. Then the result of `f` is unpredictable, and the code is hard to understand and to debug, and also the compiler cannot do optimizations.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 6:56pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/15 "2021-03-29T18:56:05Z")

</div>

What if all code is in the global scope? Is this good or bad? I do not do this.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 6:58pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/16 "2021-03-29T18:58:24Z")

</div>

> [@marllos](#):
>
> Is this good or bad

bad.

You will have all the problems mentioned.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 7:15pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/17 "2021-03-29T19:15:20Z")

</div>

That’s right, I have. I use include(“program.jl”) because of PyPlot, because the first run of the plot is very long and I have to test the code several times. Then, when there are constants in the global scope, Julia keeps giving me warnings, so I change constants to variables! Another thing that I think is bad is passing too many parameters in functions in several functions.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 29, 2021, 7:24pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/18 "2021-03-29T19:24:09Z")

</div>

Too many parameters for functions is not bad in principle. The thing is to organize them, most likely in a `struct`.

You do not need to restart the REPL every time. Take a look at these tips: [Development workflow · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/workflow/) and [Modules and Revise · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/modules/)

But, if if you are plotting stuff only, you can do everyting in global scope just fine. The peformance tips are important for code that needs performance. Otherwise one can use the flexibility of the dynamic typing as one wants.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 7:37pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/19 "2021-03-29T19:37:19Z")

</div>

The test version of the code runs 1 or 2 days, the official version runs 7 or more, so I need everything about performance. Thanks a lot for the tips.

---

<div class="post-metadata">

**Author:** ![marllos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marllos/32/205917_2.png) [@marllos](https://discourse.julialang.org/u/marllos)\
**Post date:** [March 29, 2021, 7:38pm UTC](https://discourse.julialang.org/t/are-the-scope-rules-applied-correctly-by-julia/58175/20 "2021-03-29T19:38:24Z")

</div>

Thanks to everyone.
