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
Is there a more idiomatic way to do what I want in this case?
As always, I appreciate the feedback!