Re: [julia-users] "Passing by reference" (small) headache

2016-06-08 Thread Erik Schnetter
The statement `z=z+5` first creates a new vector holding `z+5`, then
assigns this vector to `z`, which is then not a reference to the argument
any more. To modify it, you can write `z[:] += 5` (or `z[:] = z+5`, or
`z[:] = z[:] + 5`). Note that these expressions will still create a new
vector with the value of `z+5`, but this new vector will then copied into
the existing vector.

-erik

On Wed, Jun 8, 2016 at 1:44 PM, Michele Giugliano 
wrote:

> Within a function, addressing a generic element of a vector passed to it
> results in an external modification of the vector (i.e. argument passed by
> reference not value).
>
>
> How do I obtain the same effect when using the vector operator "sum to a
> scalar" ? In short, I would like the effect of the two functions f!(z) and
> g!(z), defined below, to be the same.
>
>
> function f!(z)
>
> for i=1:length(z)
>
> z[i] = 25;
>
> end
>
> z = z + 5;
>
> end
>
>
> function g!(z)
>
> for i=1:length(z)
>
> z[i] = 30;
>
> end
>
> end
>
>
> Where is my (conceptual) mistake? I would be immensely grateful for your
> help and guidance.
>



-- 
Erik Schnetter 
http://www.perimeterinstitute.ca/personal/eschnetter/


Re: [julia-users] "Passing by reference" (small) headache

2016-06-08 Thread Stefan Karpinski
Assignment and mutation are different:
http://stackoverflow.com/a/33003055/659248. Note that "pass by reference
" does
not mean what you think it means: pass by reference means passing a value
bound to a variable in the calling scope and changing the binding in the
calling scope if it's assigned in the called function. What Julia, Python,
Ruby, Java and JavaScript all do is called "pass by sharing
". More
info here:

https://www.cs.fsu.edu/~myers/c++/notes/references.html


On Wed, Jun 8, 2016 at 1:44 PM, Michele Giugliano 
wrote:

> Within a function, addressing a generic element of a vector passed to it
> results in an external modification of the vector (i.e. argument passed by
> reference not value).
>
>
> How do I obtain the same effect when using the vector operator "sum to a
> scalar" ? In short, I would like the effect of the two functions f!(z) and
> g!(z), defined below, to be the same.
>
>
> function f!(z)
>
> for i=1:length(z)
>
> z[i] = 25;
>
> end
>
> z = z + 5;
>
> end
>
>
> function g!(z)
>
> for i=1:length(z)
>
> z[i] = 30;
>
> end
>
> end
>
>
> Where is my (conceptual) mistake? I would be immensely grateful for your
> help and guidance.
>


[julia-users] "Passing by reference" (small) headache

2016-06-08 Thread Michele Giugliano


Within a function, addressing a generic element of a vector passed to it 
results in an external modification of the vector (i.e. argument passed by 
reference not value).


How do I obtain the same effect when using the vector operator "sum to a 
scalar" ? In short, I would like the effect of the two functions f!(z) and 
g!(z), defined below, to be the same.


function f!(z) 

for i=1:length(z) 

z[i] = 25; 

end 

z = z + 5; 

end


function g!(z) 

for i=1:length(z) 

z[i] = 30; 

end 

end


Where is my (conceptual) mistake? I would be immensely grateful for your 
help and guidance.