Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In julia functions : passed by reference or value?

Tags:

julia

In julia how do we know if a type is manipulated by value or by reference?

In java for example (at least for the sdk):

  • the basic types (those that have names starting with lower case letters, like "int") are manipulated by value

  • Objects (those that have names starting with capital letters, like "HashMap") and arrays are manipulated by reference

It is therefore easy to know what happens to a type modified inside a function.

I am pretty sure my question is a duplicate but I can't find the dup...

EDIT

This code :

function modifyArray(a::Array{ASCIIString,1})
    push!(a, "chocolate")
end

function modifyInt(i::Int)
    i += 7
end

myarray = ["alice", "bob"]
modifyArray(myarray)
@show myarray

myint = 1
modifyInt(myint)
@show myint

returns :

myarray = ASCIIString["alice","bob", "chocolate"]
myint = 1

which was a bit confusing to me, and the reason why I submitted this question. The comment of @StefanKarpinski clarified the issue.

My confusion came from the fact i considred += as an operator , a method like push! which is modifying the object itself . but it is not.

i += 7 should be seen as i = i + 7 ( a binding to a different object ). Indeed this behavior will be the same for modifyArray if I use for example a = ["chocolate"].

like image 215
Issam T. Avatar asked Aug 13 '16 20:08

Issam T.


People also ask

How do functions work in Julia?

Julia function arguments follow a convention sometimes called "pass-by-sharing", which means that values are not copied when they are passed to functions. Function arguments themselves act as new variable bindings (new locations that can refer to values), but the values they refer to are identical to the passed values.

What is the type of a function in Julia?

Method Tables Every function in Julia is a generic function. A generic function is conceptually a single function, but consists of many definitions, or methods. The methods of a generic function are stored in a method table.

What is ref in Julia?

In Julia, Ref objects are dereferenced (loaded or stored) with []. Creation of a Ref to a value x of type T is usually written Ref(x). Additionally, for creating interior pointers to containers (such as Array or Ptr), it can be written Ref(a, i) for creating a reference to the i-th element of a.

How do you declare a variable in Julia?

Variables in Julia can be declared by just writing their name. There's no need to define a datatype with it. Initializing variables can be done at the time of declaring variables. This can be done by simply assigning a value to the named variable.


1 Answers

The corresponding terms in Julia are mutable and immutable types:

  • immutable objects (either bitstypes, such as Int or composite types declared with immutable, such as Complex) cannot be modified once created, and so are passed by copying.

  • mutable objects (arrays, or composite types declared with type) are passed by reference, so can be modified by calling functions. By convention such functions end with an exclamation mark (e.g., sort!), but this is not enforced by the language.

Note however that an immutable object can contain a mutable object, which can still be modified by a function.

This is explained in more detail in the FAQ.

like image 149
Simon Byrne Avatar answered Sep 30 '22 01:09

Simon Byrne