I am trying to create a function to update a Float64 argument. The example below should be able to clarify what I am trying to achieve.
a=1.2
function fun!(a)
a=3.4;
end
Unfortunately, a
is updated only in local scope. Is there a way to do it? I thought that passing a pointer to the function could help, but I am not sure how to do it in Julia.
You can't do this. A Float64
is not a mutable value, so you cannot mutate the value of a
. You can only replace a
by a separate Float64
. This is what an immutable value is.
More lower level (and usually true, although there are exceptions): Float64
s are represented by their actual bytes, while mutables are pointers to the actual bytes. The actual value of a mutable is its pointer. Mutating means changing values at the memory location that the pointer is pointing to, but this doesn't exist for the immutable.
To complete the answer and if you have a C/C++ background:
Also, AFAIK, the !
of fun!
is only a name convention to draw attention it has nothing to do with julia internals. You can write fun
is you want.
Examples:
v=ones(3); # [1 1 1]
isimmutable(v) # false -> v is passed by reference
foo(v::Array{Float64}) = v .*= 2; # hence v can be modified
foo(v);
v # [2 2 2]
v=Int(1) # 1
isimmutable(v) # true -> v is passed by copy
foo(v::Int) = v *= 2 # use a local copy of v
foo(v) # returns 2, but it is a local copy
v # 1 (unmodified because we worked with a copy)
Also see FAQ
Ref{} example:
Isaiah Norton comment
foo(r::Ref{Int}) = r[] *= 2
r=Ref{Int}(1) # Base.RefValue{Int64}(1)
foo(r);
r # Base.RefValue{Int64}(2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With