Julia function argument by reference -


the docs say

in julia, arguments functions passed reference.

so quite surprised see difference in behaviour of these 2 functions:

function foo!(r::array{int64})                                                                                                                                                                                              r=r+1                                                                                                                                                                                                       end   function foobar!(r::array{int64})                                                                                                                                                                                           i=1:length(r)                                                                                                                                                                                                           r[i]=r[i]+1                                                                                                                                                                                                 end                                                                                                                                                                                                         end  

here unexpectedly different output:

julia> myarray 2-element array{int64,1}:  0  0  julia> foo!(myarray);  julia> myarray 2-element array{int64,1}:  0  0  julia> foobar!(myarray);  julia> myarray 2-element array{int64,1}:  1  1 

if array passed reference, have expected foo! change zeros ones.

r=r+1 assignment statement, means reallocates r, no longer refers pair in parent scope. r[i]=r[i]+1 mutates r value, mutation differ assignment (a description here), , after r still refers pair variable in parent scope.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

post - imageshack API cURL -

dataset - MPAndroidchart returning no chart Data available -