This is aliasing. Almost all languages allow this. -- John
Sent from my iPhone > On Dec 26, 2014, at 2:49 PM, Bradley Setzler <[email protected]> > wrote: > > Hi, > > I cannot explain this behavior. I apply a function to a variable in the > workspace, the function initializes its local variable at the workspace > variable, then modifies the local variable and produces the desired output. > However, it turns out the Julia modifies both the local and workspace > variable with each operation on the local variable. Only the local variable > is supposed to be modified. > > This is very dangerous behavior, as Julia is modifying the data itself > between performing operations on the data; the data itself is supposed to > remain fixed between operations on it. > > Minimal working example: > > data=[1,2,3] > function square(arg) > inner_var = arg > for i=1:length(inner_var) > inner_var[i] = inner_var[i]^2 > end > return inner_var > end > output=square(data) > > julia> print(data) > [1,4,9] > > The data has been squared due to the local variable, which was initialized at > the data values, being squared. Now, if i wish to apply a different function > to the data, the result will be incorrect because the data has been modified > unintentionally. > > How long has Julia been doing this? Was this behavior intentional? > Bradley > >
