# Why it does not modify vector in place

**URL:** <https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398>\
**Category:** New to Julia\
**Created:** [July 1, 2020, 9:30pm UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398 "2020-07-01T21:30:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [July 1, 2020, 9:30pm UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398/1 "2020-07-01T21:30:01Z")

</div>

Hi,

I was confused with the following function that modify the vector “as a whole” v.s. the link [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Pre-allocating-outputs-1). My function does not change the vector. Why will it behave differently?

Thank you everybody for the help.

```nohighlight
a = randn(100)
copy_of_a = copy(a)
function modified!(a, copy_of_a)
    a = randn(100)
    sum(a .!= copy_of_a) #inside the function it was modified
end
modified!(a, copy_of_a)
sum(a .!= copy_of_a) #but outside the function a still is the original

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 1, 2020, 9:41pm UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398/2 "2020-07-01T21:41:18Z")

</div>

Your function is binding the symbol `a` to a new array, not mutating the outside one. Here’s a way to see it:

```julia
julia> @show a[1] objectid(a); # old
a[1] = 0.9312276078859445
objectid(a) = 0x0835e8e05a663b75

julia> function modified!(a, copy_of_a)
           @show a[1] objectid(a) # old
           a = randn(100)
           @show a[1] objectid(a) # new
           sum(a .!= copy_of_a) #inside the function it was modified
       end
modified! (generic function with 1 method)

julia> modified!(a, copy_of_a)
a[1] = 0.9312276078859445
objectid(a) = 0x0835e8e05a663b75
a[1] = 0.18746827198090105
objectid(a) = 0x828f4d79978ef50d
100

```

If you wrote `a .= randn(100)` or `copyto!(a, rand(100))`, or just `a .= randn.()`, then instead the values inside `a` would be altered.

---

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [July 1, 2020, 9:54pm UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398/3 "2020-07-01T21:54:08Z")

</div>

Thank you. That makes sense. But I still have some confusion.

1. Why it decided to make a new local variable instead of mutate in place as element-wise operation?
2. The way you pointed out `a .= randn(100)`, will that allocate an array for randn(100) first? Then element wise assigns to a?

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [July 1, 2020, 9:57pm UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398/4 "2020-07-01T21:57:17Z")

</div>

This avoids allocating the temporary for `randn` while modifying the `Vector{Float64}(undef, 100)`:

```julia
Vector{Float64}(undef, 100) .= randn.()

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 1, 2020, 10:46pm UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398/5 "2020-07-01T22:46:05Z")

</div>

The explanation in the FAQ may solve your confusion:

[https://docs.julialang.org/en/v1/manual/faq/#I-passed-an-argument-x-to-a-function,-modified-it-inside-that-function,-but-on-the-outside,-the-variable-x-is-still-unchanged.-Why?-1](https://docs.julialang.org/en/v1/manual/faq/#I-passed-an-argument-x-to-a-function,-modified-it-inside-that-function,-but-on-the-outside,-the-variable-x-is-still-unchanged.-Why?-1)

> [@Donut\_Meepo](#):
>
> The way you pointed out `a .= randn(100)` , will that allocate an array for randn(100) first?

Yes, if you want to avoid allocation of a vector as great as `a` on the right hand side, you can loop over the values of `a`.

---

<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:** [July 2, 2020, 2:27am UTC](https://discourse.julialang.org/t/why-it-does-not-modify-vector-in-place/42398/6 "2020-07-02T02:27:55Z")

</div>

> [@heliosdrm](#):
>
> Yes, if you want to avoid allocation of a vector as great as `a` on the right hand side, you can loop over the values of `a` .

You could also do `a .= randn.()` or (equivalently) `@. a = randn()`
