See:

http://julia.readthedocs.org/en/latest/manual/faq/#i-passed-an-argument-x-to-a-function-modified-it-inside-that-function-but-on-the-outside-the-variable-x-is-still-unchanged-why

http://julia.readthedocs.org/en/latest/manual/faq/#why-does-x-y-allocate-memory-when-x-and-y-are-arrays

On Tue, Feb 17, 2015 at 10:54 AM, David Higgins <[email protected]>
wrote:

>
> In Julia, all arguments to functions are passed by reference.
>
>
> Is this really true? I know it's written in the multi-dimensional arrays
> section of the manual but it's a pretty broad statement. I was trying to
> debug some code, the issue turns out to have nothing to do with how values
> are passed to functions, but I got a bit frustrated by the following
> behaviour.
>
> This one works
> type TType
>   val :: Int;
> end
>
> function boo(tt :: TType)
>   tt.val = 7
> end
>
> t = TType(20)
> t.val
> boo(t)
> t.val
>
> This one doesn't
> function hoo(d :: Int)
>      d += 1;
> end
>
> a = 2
> hoo(a)
> a
>
>
> Now let's go crazy:
> function goo(s)
>            s += 1;
> end
>
> function zoo(w)
>            w[1] += 1;
>            w[2] -= 1;
> end
>
> d = [1 2]
> goo(d)
> d
> zoo(d)
> d
>
> Part of me 'gets it'. But on another level I find this a little
> inconsistent (I have similar issues with some of the variable scoping
> rules). I'm not necessarily opposed to the choices which have been made but
> I find that they're not really expanded upon in the manual in sufficient
> detail.
>
> Have I missed something? (I'm looking for pointers to make my life easier,
> I'm not here to whinge about the frankly sterling work the developers are
> doing).
>
> Thanks,
> David.
>
>
>

Reply via email to