how to do inplace update without a return?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how to do inplace update without a return?

Hongbo Rong
In Julia, if I pass in an array to a function, and change it inside it, it does not change the array in  the caller side. For example:
 
julia> function foo(x) x=[1 1] end
foo (generic function with 1 method)
 
julia> x=[0 0]
1x2 Array{Int64,2}:
0 0
 
julia> foo(x)
1x2 Array{Int64,2}:
1 1
 
julia> x
1x2 Array{Int64,2}:
0 0
 
how can I make x changes without using the syntax "x=foo(x)", but just "foo(x)"?

Thanks,
Hongbo
 
Reply | Threaded
Open this post in threaded view
|

Re: how to do inplace update without a return?

John Myles White
http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/

This kind of question is probably better on julia-users or Stack Overflow.

 -- John

On Aug 31, 2015, at 10:26 PM, Hongbo Rong <[hidden email]> wrote:

In Julia, if I pass in an array to a function, and change it inside it, it does not change the array in  the caller side. For example:
 
julia> function foo(x) x=[1 1] end
foo (generic function with 1 method)
 
julia> x=[0 0]
1x2 Array{Int64,2}:
0 0
 
julia> foo(x)
1x2 Array{Int64,2}:
1 1
 
julia> x
1x2 Array{Int64,2}:
0 0
 
how can I make x changes without using the syntax "x=foo(x)", but just "foo(x)"?

Thanks,
Hongbo