# Misunderstood scope rules?

**URL:** https://discourse.julialang.org/t/misunderstood-scope-rules/53097
**Category:** General Usage
**Created:** [January 9, 2021, 10:19pm UTC](https://discourse.julialang.org/t/misunderstood-scope-rules/53097 "2021-01-09T22:19:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![austinbean](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@austinbean](https://discourse.julialang.org/u/austinbean)
#### Post date: [January 9, 2021, 10:19pm UTC](https://discourse.julialang.org/t/misunderstood-scope-rules/53097/1 "2021-01-09T22:19:16Z")

</div>

I think I’m misunderstanding something about variable scopes within functions and I would like some feedback.

Here is a MWE:

```julia
# this function modifies its argument 
# and the argument 'stays modified' outside of f1
function f1(x::Array)
    for i = 1:10
        x .+= 1
    end 
end 

a = zeros(50);
f1(a)
println("a: ", a) # as expected - a = [10.0, ..., 10.0]

```

But this case does **not** work as I expected:

```julia
# this function modifies x and y within f2
# but y does not 'stay modified' outside the scope of f2
function f2(x::Array, y::Array )
    for i = 1:2
        f1(x)
        y += x # this line is not working as I thought it would.
    end 
end 

b = zeros(50);
c = zeros(50);
f2(b,c)
println("c: ", c) # expected c = [30,...,30] but c = [0, 0, ..., 0] 

```

What is the correct way to think about about the sum of `y` and `x` _inside_ `f2` ?

Is there a more idiomatic way to do what I want in this case?

As always, I appreciate the feedback!

---

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [January 9, 2021, 10:29pm UTC](https://discourse.julialang.org/t/misunderstood-scope-rules/53097/2 "2021-01-09T22:29:06Z")

</div>

```julia
y .+= x 

```

You forgot a dot. Your example creates a local variable y and binds the result of y+x to it. Using the dot modifies in place y.

---

<div class="post-metadata">

### Author: ![austinbean](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@austinbean](https://discourse.julialang.org/u/austinbean)
#### Post date: [January 10, 2021, 6:27pm UTC](https://discourse.julialang.org/t/misunderstood-scope-rules/53097/3 "2021-01-10T18:27:16Z")

</div>

Thanks very much for the reply. An easy fix too!

Is the correct way to understand what’s going on that `y += x` (no dot) creates a _new_ local variable `y` (w/ the same name as the argument of the function), while `y .+= x` does not create a new local variable?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [January 10, 2021, 8:14pm UTC](https://discourse.julialang.org/t/misunderstood-scope-rules/53097/4 "2021-01-10T20:14:02Z")

</div>

Well, be careful. `y += x` is a short notation for

```julia
y = (y + x) # parentheses are just for clarity

```

The sum in parentheses creates a new local, and the equality assigns the label `y` to that new local.

With the dot you have a concise notation for

```julia
for i in 1:length(x)
  y[i] = y[i] + x[i]
end

```

Which mutates the values of `y`.

These explanations extracted from previous questions help:  
[https://m3g.github.io/JuliaCookBook.jl/stable/assignment/](https://m3g.github.io/JuliaCookBook.jl/stable/assignment/)
