# Scope of variable - is this expected?

**URL:** <https://discourse.julialang.org/t/scope-of-variable-is-this-expected/3227>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 15, 2017, 2:24am UTC](https://discourse.julialang.org/t/scope-of-variable-is-this-expected/3227 "2017-04-15T02:24:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiagogps93](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@Thiagogps93](https://discourse.julialang.org/u/Thiagogps93)\
**Post date:** [April 15, 2017, 2:24am UTC](https://discourse.julialang.org/t/scope-of-variable-is-this-expected/3227/1 "2017-04-15T02:24:59Z")

</div>

I found this very weird. When I call f(y), the global value of y changes. Apparently, the line:  
`y[y.<0] = 0.01` is running globally. This doesn’t make sense to me.  
Is it suppose to be like this?

```julia
function f(y)
  y[y.<0] = 0.01
  return mean(y)
end

srand(1234)
y = rand(Normal(),100)

minimum(y) # -3.21
calculated_mean = f(y)
minimum(y) # 0.01

```

---

<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:** [April 15, 2017, 2:38am UTC](https://discourse.julialang.org/t/scope-of-variable-is-this-expected/3227/2 "2017-04-15T02:38:37Z")

</div>

This has nothing to do with scope. Arrays are passed by reference in Julia, so the function can change the contents of the caller’s arrays. (This is essential for high performance in many algorithms; otherwise it would be impossible to write in-place functions.) See [https://docs.julialang.org/en/latest/manual/functions.html#Argument-Passing-Behavior-1](https://docs.julialang.org/en/latest/manual/functions.html#Argument-Passing-Behavior-1)

The behavior might be clearer if you renamed your variables:

```julia
function f(y)
    y[y.<0] = 0.01
    return y
end

z = [-1,-2,3,4.0]
f(z)
println(z)

```

outputs `[0.01,0.01,3.0,4.0]`: the array `z` was changed by `f(z)`, because the argument `y` is a reference to the same underlying array.

(By convention, if a Julia function modifies its arguments, you name it to end with `!`. i.e. you would normally call your function above `f!(y)` rather than `f(y)`. This is merely a custom, however.)

This is different from Matlab, which behaves as if passing an array argument made a copy of the array. (Internally, it actually only makes a copy if the array is written to.) However, Matlab is unusual here; almost every other mainstream language works more like Julia.

It is also instructive to compare to the following function:

```julia
function g(y)
    y = [x < 0 ? 0.01 : x for x in y]
    return y
end

z = [-1,-2,3,4.0]
g(z)
println(z)

```

which prints `[-1.0,-2.0,3.0,4.0]` — the caller’s array `z` was _not_ modified by `g`, even though `g(z)` returns the same thing as `f(z)`. The reason for this is that the assignment `y = [...]` does not change the contents of the _existing_ array `y`, it instead allocates a _new_ array `[...]` and makes the variable `y` “point” to the new array instead.

(Again, this is not unique to Julia. In most programming languages, you have to distinguish between the underlying data and the variable name that is “bound” to it or “points” to it. You can have multiple variable names pointing/referring/bound to the same underlying data X, and you can change a variable to point to different data Y without affecting variables that point to X.)

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [April 15, 2017, 2:39am UTC](https://discourse.julialang.org/t/scope-of-variable-is-this-expected/3227/3 "2017-04-15T02:39:07Z")

</div>

> <https://stackoverflow.com/questions/33002572/creating-copies-in-julia-with-operator>
