I found something strange. In this code, vecadd1 doesn’t change c even if I put the ! before (c,a,b,N). However, vecadd2 will change c even if I remove ! from the function. The two functions do the same, but vecadd1 is a vectorized form, while vecadd2 is a devectorized form. I understand that Arrays are always visible from the caller, so vecadd2 is the right behavior. What is going on with vecadd1?
module VectorizedTest # vectorized # CAUTION: This won't change c function vecadd1(c,a,b,N) for i = 1:N c = a .+ b; end end # devectorized: works better than vectorized code # CAUTION: this will change c function vecadd2!(c,a,b,N) for i = 1:N, j = 1:length(c) c[j] = a[j] + b[j] end end A = rand(2) B = rand(2) C = zeros(2) N = 100000000 println(C) time1()=@time vecadd1(C,A,B,N) time1() println(C) A = rand(2) B = rand(2) C = zeros(2) N = 100000000 println(C) time2()=@time vecadd2!(C,A,B,N) time2() println(C) end