# Suprising assignment behavior

**URL:** https://discourse.julialang.org/t/suprising-assignment-behavior/1591
**Category:** General Usage
**Created:** [January 19, 2017, 4:33pm UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591 "2017-01-19T16:33:51Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![s-broda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-broda/32/3946_2.png) [@s-broda](https://discourse.julialang.org/u/s-broda)
#### Post date: [January 19, 2017, 4:33pm UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/1 "2017-01-19T16:33:51Z")

</div>

This is confusing (on 0.5.0): imagine calling a function that returns 2 values in a loop:

```julia
julia> f()=1,2
f (generic function with 1 method)

julia> a,b=f()
(1,2)

julia> a
1

julia> b
2
julia> u=v=zeros(2,1);i=1;

julia> u[i],v[i]=f()
(1,2)

```

I was surprised by this:

```julia
julia> u
2×1 Array{Float64,2}:
 2.0
 0.0

julia> v
2×1 Array{Float64,2}:
 2.0
 0.0

```

I suppose this is related to the immutability of tuples and not a bug, but it is really surprising.

Edit: Never mind. u and v are the same arrays of course. How does one delete posts?

---

<div class="post-metadata">

### Author: ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)
#### Post date: [January 19, 2017, 4:40pm UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/2 "2017-01-19T16:40:41Z")

</div>

> [@s-broda](#):
>
> u=v=zeros(2,1)

Your problem is here… `u` and `v` point to the same exact matrix, so when `u[i],v[i]=f()` is unpacked, it first sets the matrix item to `1`, and then to `2`. Since there’s only one matrix, they both show the `2`.

---

<div class="post-metadata">

### Author: ![s-broda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/s-broda/32/3946_2.png) [@s-broda](https://discourse.julialang.org/u/s-broda)
#### Post date: [January 19, 2017, 4:41pm UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/3 "2017-01-19T16:41:53Z")

</div>

Thanks. Just figured it out myself but you beat me to it 🙂

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 19, 2017, 11:01pm UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/4 "2017-01-19T23:01:02Z")

</div>

When a solution has been found and posted (even if by yourself), please mark the relevant post ad the solution by pressing the corresponding button.

---

<div class="post-metadata">

### Author: ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)
#### Post date: [January 20, 2017, 9:38am UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/5 "2017-01-20T09:38:07Z")

</div>

> [@dpsanders](#):
>
> the solution by pressing the corresponding button.

and the corresponding button is? I see ‘bookmark’, ‘share’, ‘flag’ and ‘reply’ and ‘like’…

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 20, 2017, 9:46am UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/6 "2017-01-20T09:46:42Z")

</div>

There should be one with a check mark (tick mark).

---

<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: [January 20, 2017, 9:47am UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/7 "2017-01-20T09:47:29Z")

</div>

Presumably, only the OP can see that box.

---

<div class="post-metadata">

### Author: ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)
#### Post date: [January 20, 2017, 9:55am UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/8 "2017-01-20T09:55:13Z")

</div>

That would make some sense, but even on the threads i started i don’t see this? Might be a browser issue?

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 20, 2017, 10:06am UTC](https://discourse.julialang.org/t/suprising-assignment-behavior/1591/9 "2017-01-20T10:06:13Z")

</div>

Ah, that could be true.
