# Question about global variable behavior

**URL:** <https://discourse.julialang.org/t/question-about-global-variable-behavior/86956>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 8, 2022, 5:34pm UTC](https://discourse.julialang.org/t/question-about-global-variable-behavior/86956 "2022-09-08T17:34:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)\
**Post date:** [September 8, 2022, 5:34pm UTC](https://discourse.julialang.org/t/question-about-global-variable-behavior/86956/1 "2022-09-08T17:34:28Z")

</div>

Hello!

I am trying to code Cramer’s rule from linear algebra in Julia. I am running into some variable behavior I don’t quite understand. Here’s the code (forgive the lack of “verbatim” I’m not sure how to format in discourse):

using LinearAlgebra

A = Matrix([1 4 7; 2 8 5; 3 6 9;])  
if det(A) == 0  
return “not invertible, cannot use Cramer’s Rule”  
else  
# set b = something  
b = [-1; 0; 1]  
m = size(A)[1]  
x =   
for k in 1:m  
global x  
global A  
println(“matrix A”)  
display(A)  
An = A  
An[:,k] = b  
println(“Matrix An”)  
display(An)  
xn = det(An)/det(A)  
x = push!(x,xn)  
display(x)  
end  
end

I’m confused by the behavior of A. When I change An matrix A also changes. That’s not good! I tried looking up static variables in Julia but these don’t seem to work like Python.

Can anyone explain what’s going on here and how I fix it?

Gratefully, Brian

EDIT: I’ve fixed Cramer’s rule, but only … _inelegantly_. Really hoping to understand why setting An = A then changing An also changes A… I think this is called a pointer? Forgive me, I’m not experienced.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [September 8, 2022, 5:51pm UTC](https://discourse.julialang.org/t/question-about-global-variable-behavior/86956/2 "2022-09-08T17:51:33Z")

</div>

1. To format code wrap it between triple ticks:  
```  
at the beginning and at the end  
```

2. This is not aboout global variables. When you do An = A you are giving a new name (An) to the same object in memory as A. So when you modify one you modify the other one as well.

This behavior is mentioned for example in the second point of this list: [Noteworthy Differences from other Languages · The Julia Language](https://docs.julialang.org/en/v1/manual/noteworthy-differences/)

By the way this works the same in python, maybe you were thinking Matlab?:

```python

In [3]: a = [[1, 1, 1], [2, 2, 2]]

In [4]: a
Out[4]: [[1, 1, 1], [2, 2, 2]]

In [5]: a1 = a

In [6]: a1[1][1] = 5

In [7]: a1
Out[7]: [[1, 1, 1], [2, 5, 2]]

In [8]: a
Out[8]: [[1, 1, 1], [2, 5, 2]]
```

---

<div class="post-metadata">

**Author:** ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)\
**Post date:** [September 8, 2022, 6:47pm UTC](https://discourse.julialang.org/t/question-about-global-variable-behavior/86956/3 "2022-09-08T18:47:54Z")

</div>

Thanks for the tip(s)!

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [September 8, 2022, 6:48pm UTC](https://discourse.julialang.org/t/question-about-global-variable-behavior/86956/4 "2022-09-08T18:48:36Z")

</div>

As a previous Matlab user, it is strange to me that not copying is the default. I never want to rename a variable without copying it. Why would I want two different names for the same object?

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [September 8, 2022, 7:27pm UTC](https://discourse.julialang.org/t/question-about-global-variable-behavior/86956/5 "2022-09-08T19:27:48Z")

</div>

> [@Nathan\_Boyer](#):
>
> Why would I want two different names for the same object?

One reason for me is typing convenience. Suppose I have a long-named struct `long_named_struct` containing an array `sparams`. If I pass the struct to a function, it may be more convenient to work with the array in the function as follows:

```julia
function func!(long_named_struct, x, y, ...)
    sp = long_named_struct.sparams # Short local name for use in func!
    ...
end

```
