Misunderstood scope rules?

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

Here is a MWE:

# 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:

# 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!

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.

4 Likes

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?

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

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

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/

2 Likes