How to change the value of an entire argument passed as a reference in Julia

Asked

Viewed 66 times

1

function muda_o_valor_do_inteiro!(x::Int64)
    x = 10
end

a = 9

muda_o_valor_do_inteiro!(a)
println(a) # a deveria possuir o valor 10

I have already understood that with vectors I can modify the value inside it, however I do it for integer variables, real, boolean, among others?

  • And why don’t you do it the right way and then you don’t need this?

  • You want something like unsafe_store?

1 answer

1


I’m not an expert on Julia but for what I researched it has no way to do this and should not do it, even this code does not make sense, if you want to generate a result give a return and if you want to change the value of the variable that was used in the function call then assign it to the function result.

If it’s still that important to pass something by reference, which I doubt, then use a container with the value that can be changed, can encapsulate the value in a array and so the change within the function will be reflected outside, but it will still have to create that array, then take the value inside it. But again, it doesn’t make sense in most scenarios, especially an artificial one like this. As far as I know there is no way to change the behavior of an argument directly by value.

  • I was seeing here, yeah possible pick the pointer of a variable using myvar[]. I didn’t see how to set this value. I also question the usefulness of this, since Julia allows to return tuples easily

  • That’s what I said.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.