# Global variable issue

**URL:** <https://discourse.julialang.org/t/global-variable-issue/108288>\
**Category:** General Usage\
**Tags:** question, scope, while-for-scope\
**Created:** [January 3, 2024, 9:11am UTC](https://discourse.julialang.org/t/global-variable-issue/108288 "2024-01-03T09:11:29Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![R.T.A.M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r.t.a.m/32/22289_2.png) [@R.T.A.M](https://discourse.julialang.org/u/R.T.A.M)\
**Post date:** [January 3, 2024, 11:23pm UTC](https://discourse.julialang.org/t/global-variable-issue/108288/21 "2024-01-03T23:23:08Z")

</div>

I found a nice way to check the scopes of functions and variables that have been run from VScode to the terminals REPL. Use varInfo() in the REPL to get a report on what modules have been loaded. Then use .tab completion to track down where the targets scope is.

This is handy if queries like `@info myvar` return an UndefVarError.

```julia
julia> varInfo() 
  name size summary
  –––––––––––––––– ––––––––––– –––––––
  Base Module
  Core Module
  InteractiveUtils 551.066 KiB Module
  Main Module
  VXITest 119.475 KiB Module

julia> VXITest.<tab>
VXI
eval
include
ip
julia> VXITest.ip
"192.168.0.40"

julia> VXITest.VXI.<tab>
...
eval include
ip    
...

julia> VXITest.VXI.ip
"192.168.0.40"

```

Aha! What’s this other ip variable-with-the-same-name-as-a-variable-declared-in-the-module-that-included-it? Could be a problem.  
I’ve just been translating some code from C# to Julia and the scope thing has caused me to go back to Julia’s manual a lot.

---

<div class="post-metadata">

**Author:** ![R.T.A.M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/r.t.a.m/32/22289_2.png) [@R.T.A.M](https://discourse.julialang.org/u/R.T.A.M)\
**Post date:** [January 3, 2024, 11:27pm UTC](https://discourse.julialang.org/t/global-variable-issue/108288/22 "2024-01-03T23:27:56Z")

</div>

Also try ‘ALT-enter’ which runs the whole thing.  
‘CTRL-enter’ will eval the line the cursor is on or the function block its within.

---

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 3, 2024, 11:57pm UTC](https://discourse.julialang.org/t/global-variable-issue/108288/23 "2024-01-03T23:57:04Z")

</div>

```julia
    basis[1:s1-1,s+1]=basis[1:s1-1,s]
    basis[s1,s+1]=basis[s1,s]-1
    basis[s1+1,s+1]=N-sum(basis[1:s1,s+1])

```

so in these codes, ‘basis’ is not reassgined? I think I really need to learn more about the underlying mechanisms of computers or computer languages.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 4, 2024, 12:37am UTC](https://discourse.julialang.org/t/global-variable-issue/108288/24 "2024-01-04T00:37:06Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> so in these codes, ‘basis’ is not reassgined?

Nope, indexed assignment only mutates the instance assigned to `basis`, but the instance’s elements are being reassigned. By the way, MATLAB [works the same way](https://www.mathworks.com/matlabcentral/answers/884454-what-is-the-difference-between-assigning-with-and-without-range), though its variables are a bit different from Julia’s in other ways.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 4, 2024, 12:53am UTC](https://discourse.julialang.org/t/global-variable-issue/108288/25 "2024-01-04T00:53:28Z")

</div>

`basis` is not reassigned. It is the same array. You are just mutating it.

Note that in the first line `basis[1:s1-1,s]` this makes a new `Matrix` of size `(s1-1,1)`. You might want to consider using `@view` here because we don’t actually need a new `Matrix`. You just need `SubArray` referencing part of the original matrix.

---

<div class="post-metadata">

**Author:** ![A\_C](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@A\_C](https://discourse.julialang.org/u/A_C)\
**Post date:** [January 4, 2024, 1:54am UTC](https://discourse.julialang.org/t/global-variable-issue/108288/26 "2024-01-04T01:54:44Z")

</div>

`Alt-Enter` (as mentioned above) or the `Base.include` seem good enough to me for interactive use since the variables show up in Variable explorer.

Since, it is already possible to evaluate the file with REPL like scoping rules would it make sense to have this as an option for the run button of VS code extension.  
It could be like `Julia: Execute file in REPL soft scope (use this option for interactive use only)` and work if the file did not have other included files i.e. work for only one file scripts. If this were the default option of the Run button it would reduce the number of scope related questions from new users.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 4, 2024, 3:14am UTC](https://discourse.julialang.org/t/global-variable-issue/108288/27 "2024-01-04T03:14:23Z")

</div>

> [@A\_C](#):
>
> would it make sense to have this as an option for the run button of VS code extension.

No, we shouldn’t encourage people to use REPL rules in files, where it makes sense to have `eval`/files rules.

> [@A\_C](#):
>
> work if the file did not have other included files

This is incredibly hard to prove. I could make an alias to `include`, even import that alias from a package, so that no `include` call shows up in the source code. The soft scope rules only exist to let some local scope code be pasted into and work in the global scope or isolated notebook, not inter-`include`ing files.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [January 4, 2024, 3:32am UTC](https://discourse.julialang.org/t/global-variable-issue/108288/28 "2024-01-04T03:32:20Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> so in these codes, ‘basis’ is not reassgined

Something that may be confusing is that `=` in this code has a different meaning than in most other contexts. When you call `a[b] = c`, this is not assigning anything, it’s what we call “syntax sugar”, and actually _means_ `setindex!(a, b, c)`, which is a function that mutates `a` by changing what’s stored at index `b` to the value `c`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 4, 2024, 6:24am UTC](https://discourse.julialang.org/t/global-variable-issue/108288/29 "2024-01-04T06:24:20Z")

</div>

What Kevin said is also disoverable. In this case, we can use `@which` or `@code_lowered`.

```julia-repl
julia> A = zeros(5)
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> @which A[2] = 3
setindex!(A::Array{T}, x, i1::Int64) where T
     @ Base array.jl:1021

julia> function f(A)
           A[1] = 3
       end
f (generic function with 1 method)

julia> @code_lowered f(A)
CodeInfo(
1 ─ Base.setindex!(A, 3, 1)
└── return 3
)

```

[Previous page](https://discourse.julialang.org/t/global-variable-issue/108288.md?page=1)
