Le lundi 19 janvier 2015 à 21:34 -0800, AVF a écrit :
> I just wanted to double-check that I understand it correctly: doing a
> vectorized operation to a passed variable inside a function will not
> change the variable on the outside?
More or less. `x += 10` is just syntactic sugar for `x = x + 10`: it
allocates a new array, and binds it to x. The original x is not mutated.

> Does one have to resort to a `for` loop, or is there another way?
You can do something like this:

function foo!(x)
    x[:] += 10
end


Regards


>         function foo!(x)
>         x += 10
>         end
>          
>         function bar!(x) 
>         [ x[i] += 10 for i in 1:size(x,1) ] 
>         end
>          
>         y = [1:3]
>          
>         foo!(y)
>         println(y)
>          
>         bar!(y)
>         println(y)
> 
> 
> Output:
> 
> 
>         [1,2,3]
>         [1,2,3]
>         [11,12,13]
> 
> 
> Thanks. 

Reply via email to