# Why making a change in a copy of a variable changes the original variable?

**URL:** <https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125>\
**Category:** New to Julia\
**Created:** [November 14, 2020, 2:19am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125 "2020-11-14T02:19:57Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [November 14, 2020, 2:19am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/1 "2020-11-14T02:19:57Z")

</div>

I am so confused now. Can you explain this to me:

```julia
aa = [1,2,3]
bb = aa
bb[1,1] = 66

```

Output:

```julia
bb = 3-element Array{Int64,1}:
 66
  2
  3

aa = 3-element Array{Int64,1}:
 66
  2
  3

```

I am only making a change in bb, why aa is changing as well?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 14, 2020, 2:25am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/2 "2020-11-14T02:25:05Z")

</div>

The `=` operator does not make a copy. It is assigning a new variable (i.e., way to refer to an object) the same reference (actual object in memory). If you want a copy you can use `copy`.

```julia
julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b = copy(a)
3-element Array{Int64,1}:
 1
 2
 3

julia> a[1] = 4
4

julia> a
3-element Array{Int64,1}:
 4
 2
 3

julia> b
3-element Array{Int64,1}:
 1
 2
 3

```

The `isbitstype` like `Int` are immutable and contains no references to other values. Those work a bit a different,

```julia
julia> a = 2
2

julia> b = a
2

julia> a = 1
1

julia> a
1

julia> b
2

```

Here is the different behaviors with mutable structs.

```julia
julia> mutable struct A
           x::Int
       end

julia> a = A(2)
A(2)

julia> b = a
A(2)

julia> a = A(1)
A(1)

julia> a
A(1)

julia> b
A(2)

```

```julia
julia> a = A(2)
A(2)

julia> b = a
A(2)

julia> a.x = 1
1

julia> a
A(1)

julia> b
A(1)

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [November 14, 2020, 2:35am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/3 "2020-11-14T02:35:57Z")

</div>

This is the only language working like this. I have never seen this before. This can causes a lot of issues though. Currently, I am giving some variables into a function and I will modify them inside the function but I don’t want them to be changed in the main function. What should I do then?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 14, 2020, 2:43am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/4 "2020-11-14T02:43:21Z")

</div>

Some values are immutable. If you really want to disallow changes to arguments passed into functions, use immutable values. Arrays in a way act like pointers to mutable data. Passing an array will therefore not prevent a function from changing some of the data.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 14, 2020, 2:52am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/5 "2020-11-14T02:52:49Z")

</div>

Python and Java both do this also. What languages are you coming from?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 14, 2020, 3:04am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/6 "2020-11-14T03:04:28Z")

</div>

I think R make copies with the assignment operator. Still R, makes some really weird decisions such as auto conversion rules. For example, if you have a 1M element `integer` vector and replace an element with a `character` it will just make a new copy of the full vector with the `character` representation of the 1M elements.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [November 14, 2020, 3:08am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/7 "2020-11-14T03:08:16Z")

</div>

One should avoid creating copies if one does not need to. If you have a mutating function it is usually denoted with a bang `magic!(obj)` while `magic(obj)` would not modify `obj`.  
Try this approach

```julia
julia> function magic!(obj::AbstractVector, x)
           obj[1] = x
           obj
       end
magic! (generic function with 1 method)

julia> magic(obj::AbstractVector, x) = magic!(copy(obj), x)
magic (generic function with 1 method)

julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> magic(a, 4)
3-element Array{Int64,1}:
 4
 2
 3

julia> a
3-element Array{Int64,1}:
 1
 2
 3

```

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [November 14, 2020, 3:55am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/8 "2020-11-14T03:55:43Z")

</div>

MATLAB and Python. I didn’t know this about Python though.

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [November 14, 2020, 4:00am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/9 "2020-11-14T04:00:13Z")

</div>

So how can I pass an array or a matrix to a function without changing them in the main code?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 14, 2020, 4:01am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/10 "2020-11-14T04:01:18Z")

</div>

just use `copy` in the function so it doesn’t reference the same objects that you pass in.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 14, 2020, 4:02am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/11 "2020-11-14T04:02:50Z")

</div>

But remember that one of the basic tenets of Julia is that copying is in general inefficient. If I have a large array, it is inefficient to make a copy, change the copy inside the function, and return it. If I can justify changing an array inside a function, I will do it, because it will avoid the cost of the copying.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 14, 2020, 5:16am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/12 "2020-11-14T05:16:01Z")

</div>

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

In [2]: b = a

In [3]: b[0] = 10

In [4]: a
Out[4]: [10, 2, 3]

```

yeah this is almost always what users want other wise it’s too painful…

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 14, 2020, 9:23am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/13 "2020-11-14T09:23:30Z")

</div>

> [@Nova](#):
>
> MATLAB and Python. I didn’t know this about Python though.

Yeah, this is exactly the same as what Python does. Did you try your own example in Python?

---

<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:** [November 14, 2020, 9:43am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/14 "2020-11-14T09:43:14Z")

</div>

Is Matlab not also working the same way? It is a long time ago when I last used it and I do not have a license anymore to check.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 14, 2020, 9:52am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/15 "2020-11-14T09:52:41Z")

</div>

> [@Nova](#):
>
> So how can I pass an array or a matrix to a function without changing them in the main code?

It is better to organize your code assuming that functions _never_ change (the contents of) their arguments unless this is explicitly documented, and thus requested by the caller.

Ideally, if your function wants to modify contents of a container, it should make a copy within the function body itself, or use a preallocated buffer. See [the `!` convention](https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention).

---

<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:** [November 14, 2020, 9:57am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/16 "2020-11-14T09:57:59Z")

</div>

> [@lungben](#):
>
> Is Matlab not also working the same way?

No, Matlab copies on assignment (it actually cheats a bit, and only makes the copy when you start modifying it).

It is one of the many frustrating things about Matlab.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 14, 2020, 10:08am UTC](https://discourse.julialang.org/t/why-making-a-change-in-a-copy-of-a-variable-changes-the-original-variable/50125/17 "2020-11-14T10:08:03Z")

</div>

R does it too, and it’s as horrible as you would expect (especially when it comes to threading, and/or other optimizations). See

> **[The Secret Lives of R Objects](https://www.brodieg.com/2019/02/18/an-unofficial-reference-for-internal-inspect/)**
>
> A guide to the undocumented, unpublicized, and occasionally useful .Internal(inspect(...)) R function.
