# Why the variable is not modified inside a function with suffix "!"

**URL:** <https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 8, 2021, 1:37pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581 "2021-06-08T13:37:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentVidal95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentvidal95/32/25915_2.png) [@LaurentVidal95](https://discourse.julialang.org/u/LaurentVidal95)\
**Post date:** [June 8, 2021, 1:37pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/1 "2021-06-08T13:37:06Z")

</div>

Dear Julia community,

I am trying to use the “!” prefix in order to modify a variable inside a function and have made this little test:

```julia
function test_copy!(a,b)
   a = b
   println("a in function: $a")
end

a = 0.0; b = 1.0;
test_copy!(a,b)
println("a outside function: $a")

```

The results are: **“a in function: 1.0”** and **“a outside function: 0.0”**.  
So the variable a has not been modified by the call to test\_copy!.

But if I do the same test with size(1) arrays

```julia
function test_equal_arrays!(vec,x)
   vec[1] = x
end

vec = [0.0]; x = 1.0
test_equal_arrays!(vec,x)
@show(vec)

```

I get **“vec = [1.0]”**.

So I am getting a little confused. Does someone knows how the “!” prefix work in that case ?  
Thanks to all

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 8, 2021, 1:41pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/2 "2021-06-08T13:41:55Z")

</div>

> [@LaurentVidal95](#):
>
> Does someone knows how the “!” prefix work in that case ?

The exclamation mark doesn’t do any work at all, it’s just a naming convention.

The difference between the two cases is that in the first case you just create a new local binding for the variable name `a`. In the second case you update the content of the mutable object that `vec` points to.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 8, 2021, 1:58pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/3 "2021-06-08T13:58:29Z")

</div>

Nitpicking here, but the `!` in `foo!()` is actually a postfix. This had me confused when I first read your title.

---

<div class="post-metadata">

**Author:** ![LaurentVidal95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentvidal95/32/25915_2.png) [@LaurentVidal95](https://discourse.julialang.org/u/LaurentVidal95)\
**Post date:** [June 8, 2021, 2:00pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/4 "2021-06-08T14:00:48Z")

</div>

Ho thanks, I didn’t realise it was only a convention!

---

<div class="post-metadata">

**Author:** ![LaurentVidal95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentvidal95/32/25915_2.png) [@LaurentVidal95](https://discourse.julialang.org/u/LaurentVidal95)\
**Post date:** [June 8, 2021, 2:02pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/5 "2021-06-08T14:02:11Z")

</div>

You’re right I’ll modify it !

---

<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:** [June 8, 2021, 2:11pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/6 "2021-06-08T14:11:17Z")

</div>

Nitpicking here, XD, but the `!` in `foo!()` is actually a suffix, because the character makes part of the token and it is not a separate operator.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 8, 2021, 2:17pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/7 "2021-06-08T14:17:53Z")

</div>

Dammit, Jesus was right about that whole casting the first stone thing 🤣

---

<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:** [June 8, 2021, 2:21pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/8 "2021-06-08T14:21:32Z")

</div>

About the difference between bindings and modifying mutable objects, there were many questions about it here in this forum, I selected some of my previous answers:

> [@When will the original value be changed with the copied value?](https://discourse.julialang.org/t/when-will-the-original-value-be-changed-with-the-copied-value/37810/6):
>
> I did not find the specific section of the manual that explains this, so I will try to explain it myself (but I am almost sure such section exists). The problem I see is that your mental model on how variables and objects work is not how they work in Julia (but I am sure in other languages you would see the behavior you did expect). In Julia, a variable is just a name for some value in some scope. When you attribute a value to some variable you are never copying it, you are binding it, this is…

> [@Does splatting create a shallow copy?](https://discourse.julialang.org/t/does-splatting-create-a-shallow-copy/55566/8):
>
> A wise decision. In practice things happens as you describe, but there is also a fundamental misunderstanding here: copy is blind to the inner elements, it does not take different decisions based on the type of the inner elements, it always do the same thing: copying a reference to the element. “But what about Ints”, you may say, “they are copied by value” or yet, “changes I make to them do not reflect in the original array”. To that I have two answers: “changes I make to them do not refle…

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [June 8, 2021, 2:38pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/9 "2021-06-08T14:38:00Z")

</div>

The confusing thing is that the equal sign in `x[1] = 2` has absolutely nothing to do with the equal sign in `x = 2`. For instance you can see with `x = randn(2); @which x[1] = 2` that this actually gets lowered to a function call.

---

<div class="post-metadata">

**Author:** ![LaurentVidal95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentvidal95/32/25915_2.png) [@LaurentVidal95](https://discourse.julialang.org/u/LaurentVidal95)\
**Post date:** [June 8, 2021, 4:37pm UTC](https://discourse.julialang.org/t/why-the-variable-is-not-modified-inside-a-function-with-suffix/62581/10 "2021-06-08T16:37:09Z")

</div>

The title was revised again 😛
