… ignore that spurious end in the code I sent. It was left over from a begin
end block in the REPL.
On Friday, September 26, 2014 7:44:02 PM UTC-7, Ethan Anderes wrote:
Stephan:
>
> Just as an addendum to Stefan and John’s comments, the following small
> change to your code will mutate x:
>
> function f!(x)
> x[:]=x+1
> y=x+1
> end
> end
>
> Notice, I put a ! at the end of the function name. This is just
> convention to warn the user that the argument may be mutated. Coming form
> Matlab, the way I reason about this is that everything behaves the same, as
> far as local scope for x is concerned. The difference is that in Matlab,
> the input array is copied and assigned to the local x, whereas in Julia
> the address of the input array is copied and assigned to the local x.
> Therefore x = x + 1 says assign x to a new address in memory (which
> wouldn’t change the global scope x since the function scope is blind to
> it, just as in Matlab). On the other hand x[:] = x + 1 says “go to where
> x is addressed and put in the value x + 1”. So, in the Julia way, the
> global scope x address hasn’t changed, but f! just scooped out the values
> living in those locations and changed them. I’m probably rambling a bit
> after a few friday night beers but I hope it helps.
>
> BTW: it is entirely possible that my working understanding is not the
> right way to explain it. Feel free, and be sure to, correct me if I’m not
> saying it right.
>
> Cheers!
> Ethan
>
> On Friday, September 26, 2014 1:51:37 PM UTC-7, Stephan Buchert wrote:
>
> From the manual
>> Argument Passing Behavior: ...means that values are not copied when they
>> are passed to functions. ...Modifications to mutable values (such as
>> Arrays) made within a function will be visible to the caller.
>>
>> I get
>> julia> function f(x)
>> x=x+1
>> y=x+1
>> end
>> f (generic function with 1 method)
>>
>> julia> x=[1:5]'
>> 1x5 Array{Int64,2}:
>> 1 2 3 4 5
>>
>> julia> y=f(x)
>> 1x5 Array{Int64,2}:
>> 3 4 5 6 7
>>
>> julia> x
>> 1x5 Array{Int64,2}:
>> 1 2 3 4 5
>>
>> Apparently Array x is copied inside the function, at least in this case,
>> and this is not visible to the caller (here made indirectly visible). The
>> manual is confusing for me. And what is the difference between my example
>> and the double! function from the manuals style guide, which does actually
>> modify the function argument?
>>
>>
>