# += operator

**URL:** https://discourse.julialang.org/t/operator/98275
**Category:** General Usage
**Created:** [May 3, 2023, 8:13pm UTC](https://discourse.julialang.org/t/operator/98275 "2023-05-03T20:13:10Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [May 3, 2023, 8:13pm UTC](https://discourse.julialang.org/t/operator/98275/1 "2023-05-03T20:13:10Z")

</div>

Is the `+=` operator used as in:

```julia
x = 5.
x += 3.

```

equivalent to

```julia
x .= x + 3.

```

or

```julia
x = x + 3

```

According to the documentation, it is equivalent to the latter ([Mathematical Operations and Elementary Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/mathematical-operations/)), at least in Julia 1.8.5

Thanks,

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 3, 2023, 8:21pm UTC](https://discourse.julialang.org/t/operator/98275/2 "2023-05-03T20:21:38Z")

</div>

It’s the latter, as the documentation describes.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [May 3, 2023, 8:22pm UTC](https://discourse.julialang.org/t/operator/98275/3 "2023-05-03T20:22:53Z")

</div>

That is very surprising because in languages such as C++, there is now overwriting. What is the rationale for this choice for the operator? What have such an operator? Thanks.

---

<div class="post-metadata">

### Author: ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)
#### Post date: [May 3, 2023, 8:23pm UTC](https://discourse.julialang.org/t/operator/98275/4 "2023-05-03T20:23:56Z")

</div>

Yes.

See [Updating Operators in the manual](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Updating-operators)

and [Vectorized Dot Operators](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 3, 2023, 8:24pm UTC](https://discourse.julialang.org/t/operator/98275/5 "2023-05-03T20:24:42Z")

</div>

Different languages make different choices - in julia, `+=` (that is, assignments) are not an operator. They don’t operate on anything, the are expressions. It’s just a difference in language design.

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [May 3, 2023, 8:26pm UTC](https://discourse.julialang.org/t/operator/98275/6 "2023-05-03T20:26:03Z")

</div>

Thanks.

---

<div class="post-metadata">

### Author: ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)
#### Post date: [May 5, 2023, 3:44pm UTC](https://discourse.julialang.org/t/operator/98275/8 "2023-05-05T15:44:10Z")

</div>

There is also

```julia
x .+= 3

```

---

<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: [May 5, 2023, 4:26pm UTC](https://discourse.julialang.org/t/operator/98275/9 "2023-05-05T16:26:29Z")

</div>

> [@cjdoris](#):
>
> There is also
> 
> ```julia
> x .+= 3
> 
> ```

This is only for mutable containers like arrays, since it is equivalent to a `broadcast!` call. It will give an error if `x` refers to a scalar (which immutable) as in the examples above.

> [@erlebach](#):
>
> That is very surprising because in languages such as C++, there is now overwriting.

I’m confused about what behavior you expect here.

```julia
x = 5.0
x += 3.0

```

does exactly the same thing in Julia as it does in C++ for `double x` variable. (In both cases, the value of `x` is now `8.0`.)

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [May 5, 2023, 6:27pm UTC](https://discourse.julialang.org/t/operator/98275/10 "2023-05-05T18:27:01Z")

</div>

Yes, but is the address (memory location) of the first `x=5`, the same as the memory address of `x` in the second line `x += 3.0`. I believe in C++ that it is. The `+=` operator is in place. From the earlier messages in this thread, julia does not update a scalar `x` in place.

---

<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: [May 5, 2023, 6:47pm UTC](https://discourse.julialang.org/t/operator/98275/11 "2023-05-05T18:47:32Z")

</div>

> [@erlebach](#):
>
> Yes, but is the address (memory location) of the first `x=5`, the same as the memory address of `x` in the second line `x += 3.0`. I believe in C++ that it is.

The **language standards** say nothing about the memory addresses of local variables (unless you specifically compute `&x` in C++, or if it is [`volatile`](https://en.wikipedia.org/wiki/Volatile_(computer_programming))), AFAIK. But in both C++ and Julia the **compiler** almost certainly updates `x` in-place with the new value. In fact, it probably puts `x` in a register — it may have _no_ “memory address”!

(Conversely, the “memory location” of a local variable can change over time, e.g. if there is a [register spill](https://en.wikipedia.org/wiki/Register_allocation), and variables can even be optimized completely out of the program. The only time a value should stay in one spot is if you explicitly store it at a memory location, e.g. by putting it in an array or a mutable struct, or if you request a pointer to it in C/C++. And in that case of course you can update that location in-place, e.g. `array[i] += 5.0` updates in-place in Julia.)

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [May 5, 2023, 6:49pm UTC](https://discourse.julialang.org/t/operator/98275/12 "2023-05-05T18:49:29Z")

</div>

For things like scalars, does it really matter if it is the same memory address or not? In numerical computing, I can’t think of any examples why it would.

---

<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: [May 5, 2023, 6:51pm UTC](https://discourse.julialang.org/t/operator/98275/13 "2023-05-05T18:51:47Z")

</div>

> [@tbeason](#):
>
> For things like scalars, does it really matter if it is the same memory address or not?

I think @erlebach is worried more about efficiency? But that is confusing language semantics for compiler optimizations. Even though _semantically_ `x = 5.0` and `x += 3.0` refer to “different objects” (since you aren’t mutating the value of `5.0`!) in Julia and in some sense in C++, in practice the compiler will almost certainly store the new value in the same location (e.g. a register, on the stack, whatever).

(Fun fact: in ancient times, with Fortran, you _could_ sometimes accidentally [“change the value of 5.0”](https://softwareengineering.stackexchange.com/questions/254799/ever-change-the-value-of-4-how-did-this-come-into-hayes-thomas-quiz).)

---

<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: [May 5, 2023, 7:03pm UTC](https://discourse.julialang.org/t/operator/98275/14 "2023-05-05T19:03:36Z")

</div>

> [@stevengj](#):
>
> In fact, it probably puts `x` in a register — it may have _no_ “memory address”!

In Julia, you can quite easily inspect the compiler output for yourself. For example:

```julia
julia> function f(y)
          x = y
          x += 3.0
          return x
       end;

julia> @code_native debuginfo=:none f(5.0). # (output edited to remove some metadata)
	fmov	d1, #3.00000000
	fadd	d0, d0, d1
	ret

```

In the compiled version of this function, there are no memory addresses! The input is passed in a register `d0`, to which `3.0` is added “in-place” with an `fadd` instruction and returned.
