# Functions with a bang!

**URL:** https://discourse.julialang.org/t/functions-with-a-bang/89032
**Category:** New to Julia
**Created:** [October 20, 2022, 7:12pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032 "2022-10-20T19:12:43Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [October 20, 2022, 7:12pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032/1 "2022-10-20T19:12:43Z")

</div>

I’m currently going through [this excellent book](https://juliadatascience.io/data_structures) on getting started with Julia, and I’ve run into a behavior I don’t quite understand. The book provides this example of a function that overwrites its inputs:

```julia
function addone!(a)
    for i in 1:length(a)
        a[i] += 1
    end
end

```

Now any input gets overwritten by adding one to it:

```julia
julia> x = [1 2 3];

julia> addone!(x);

julia> x
1×3 Matrix{Int64}:
 2 3 4

```

Above, we see that every element of `x` now has 1 added to it.

To me, the loop in the function above looks like just an unnecessary bit of clutter, so I attempted to rewrite the function as follows:

```julia
function my_addone!(a)
    a = a .+ 1
end

```

Now when I try to use `my_addone!` in the same way that I used `addone!` above, I find that the behavior is different:

```julia
julia> x = [1 2 3];

julia> my_addone!(x);

julia> x
1×3 Matrix{Int64}:
 1 2 3

```

Why is it that the function only overwrites the input if there’s a loop inside the function? I feel like I’m missing some fundamental principle.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [October 20, 2022, 7:16pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032/2 "2022-10-20T19:16:18Z")

</div>

No worries, you did well. You just missed a little dot:

```julia
function my_addone!(a)
    a .= a .+ 1
end

```

will do.

I thought I find a section about why your version copies the result, but I didn’t found it. So I just tell you that

```julia
a .+ 1

```

creates a new array, which is than assigned to `a`. … aah I give up, @lmiq does a better job 😉

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 20, 2022, 7:20pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032/3 "2022-10-20T19:20:32Z")

</div>

> [@chadagreene](#):
>
> I feel like I’m missing some fundamental principle.

With the `dot` above, the `a .= a .+ 1` is a syntax sugar for the same loop of the original function. _Without_ the dot, `a = a .+ 1` creates a new array from the result of `a .+ 1` (with the elements with 1 added), but assigns that label `a` to this new array. The fact that the label `a` was “taken” does not matter here, that is equivalent to `b = a .+ 1` where `b` is the label of the newly created array.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [October 20, 2022, 7:28pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032/4 "2022-10-20T19:28:38Z")

</div>

The best source of info about that is probably this post: [More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)

---

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [October 20, 2022, 7:29pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032/5 "2022-10-20T19:29:07Z")

</div>

Many thanks, @oheil and @lmiq! Your explanations are very clear.

I see that my attempt with `=` was just making a copy that happened to have the same name as the input `a`, whereas `.=` is shorthand that overwrites each element of `a`. Very cool!

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [October 20, 2022, 7:40pm UTC](https://discourse.julialang.org/t/functions-with-a-bang/89032/6 "2022-10-20T19:40:52Z")

</div>

To contribute nothing more than a name, this behaviour is called [variable shadowing](https://en.wikipedia.org/wiki/Variable_shadowing).
