# How for loops, map and comprehensions act on variables?

**URL:** <https://discourse.julialang.org/t/how-for-loops-map-and-comprehensions-act-on-variables/33849>\
**Category:** New to Julia\
**Created:** [January 27, 2020, 1:57pm UTC](https://discourse.julialang.org/t/how-for-loops-map-and-comprehensions-act-on-variables/33849 "2020-01-27T13:57:20Z")\
**Posts on this page:** 1\
**Showing post:** 7

<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:** [January 27, 2020, 4:03pm UTC](https://discourse.julialang.org/t/how-for-loops-map-and-comprehensions-act-on-variables/33849/7 "2020-01-27T16:03:00Z")

</div>

> [@Marco-Congedo](#):
>
> I though that it is a good practice to create memory for all variables that are used over and over again at the beginning of the function and then allowing the function to manipulate those, reusing the same allocated memory, as compared to create again and again variables on the fly as they are needed.

Preallocating arrays is often a good practice, but you can still pass them as arguments — it doesn’t require you to use globals.

In any case, as @ericphanson pointed out above, a statement like `B₊ = B * M` or `𝐃₊ = [Hermitian(M'*D*M) for D ∈ 𝐃] ` allocates new arrays anyway — you are “rebinding” the variables `B₊` and `𝐃₊` to “point” to _new_ arrays in memory, _not mutating them in-place_. See also [this discussion](https://discourse.julialang.org/t/assignment-and-mutation/19119/4).

(This is not unique to Julia! If you do `a = [3,4,5]`, then `b = a`, followed by `a = [4,5,6]` in Python or Matlab or most other languages with analogous operations, `b` will still be `[3,4,5]`.)

---

_[View the full topic](https://discourse.julialang.org/t/how-for-loops-map-and-comprehensions-act-on-variables/33849)._
