Julia scoping: why does this function modify a global variable?
问题 I'm a relative newcomer to Julia, and so far I am a fan of it. But coming from years of R programming, some scoping rules leave me perplexed. Let's take this function. This behaves exactly as I would expect. function foo1(x) y = x t = 1 while t < 1000 t += 1 y += 1 end return 42 end var = 0; foo1(var) # 42 var # 0 But when doing something similar on an array, it acts as a mutating function (modifies it argument in the global scope!) function foo2(x) y = x t = 1 while t < 1000 t += 1 y[1] += 1