# Assignment vs mutation with bang! functions

**URL:** https://discourse.julialang.org/t/assignment-vs-mutation-with-bang-functions/54466
**Category:** General Usage
**Created:** [February 2, 2021, 3:21pm UTC](https://discourse.julialang.org/t/assignment-vs-mutation-with-bang-functions/54466 "2021-02-02T15:21:07Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [February 2, 2021, 3:21pm UTC](https://discourse.julialang.org/t/assignment-vs-mutation-with-bang-functions/54466/1 "2021-02-02T15:21:07Z")

</div>

Hi, a question on mutating functions. If I have a function `sum!(A::Matrix, B::Matrix)` that adds `A` to `B` in place, due to Julia’s way of handling arrays it won’t actually overwrite `B` unless I call it in the following way: `B = sum!(A,B)` . Should I append ! to such functions anyway as they’re still mutating the function arguments in a sense? Also, does this have implications for memory allocation? Is it still an efficient function? Does `B = sum!(A,B)` still operate in place?

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1612279222399200?thread_ts=1612279222.399200&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [February 2, 2021, 3:43pm UTC](https://discourse.julialang.org/t/assignment-vs-mutation-with-bang-functions/54466/2 "2021-02-02T15:43:38Z")

</div>

> [@BridgeBot](#):
>
> If I have a function `sum!(A::Matrix, B::Matrix)` that adds `A` to `B` in place, due to Julia’s way of handling arrays it won’t actually overwrite `B` unless I call it in the following way: `B = sum!(A,B)` .

That’s not correct. If you actually modify the contents of `B` in your function, there is no need for an assignment afterwards.

---

<div class="post-metadata">

### Author: ![pablosanjose](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pablosanjose/32/7006_2.png) [@pablosanjose](https://discourse.julialang.org/u/pablosanjose)
#### Post date: [February 2, 2021, 4:14pm UTC](https://discourse.julialang.org/t/assignment-vs-mutation-with-bang-functions/54466/3 "2021-02-02T16:14:34Z")

</div>

In other words, the bang in the function name does not affect how Julia treats the function in any way. It is just a convention for the benefit of the reader.

The convention is: if a function mutates one or more of its arguments, signal that to the user and to other developers by adding a bang to the function name. Also, the convention is that the modified arguments should (if possible) come first, followed by non-mutated arguments. There is an exception to this when a function is the first argument, as in `map!(func, destination_array, ...)`. In that case it is undertood that it is the second argument that is being mutated.

Example from Julia’s build-in `sum!`

```julia
help?> sum!
search: sum! cumsum! sum summary cumsum isnumeric VersionNumber ...

  sum!(r, A)

  Sum elements of A over the singleton dimensions of r, and write results to r.
....

```
