Julia function argument by reference

前端 未结 4 1571
抹茶落季
抹茶落季 2021-02-05 08:22

The docs say

In Julia, all arguments to functions are passed by reference.

so I was quite surprised to see a difference in the behav

相关标签:
4条回答
  • 2021-02-05 09:09

    r=r+1 is an Assignment statement, this means it reallocates r, so it no longer refers to its pair in the parent scope. but r[i]=r[i]+1 Mutates r value, mutation is differ from assignment (a good description here), and after that r still refers to its pair variable in the parent scope.

    0 讨论(0)
  • 2021-02-05 09:13

    In order to mutate each variable inside an array, the broadcast . operation can be used. But be aware that each value inside the array will be changed equally thus there is no need for a for loop.

    In the case of adding 1 to each element of an array :

    a = rand(1:10, 10)
    show(a) = [4, 8, 9, 1, 4, 2, 6, 7, 1, 5]
    
    function add1!(a::Array{Int64})
        a .= a .+ 1
    end
    
    add1!(a);
    show(a) = [5, 9, 10, 2, 5, 3, 7, 8, 2, 6]
    

    Despite this, if each value of an array is needed to be changed independently then for loop with indices is inevitable.

    0 讨论(0)
  • 2021-02-05 09:18

    I think the document is a bit vague here.

    Strictly speaking, Julia is "call-by-value where the value is a reference" , or "call-by-sharing", as used by most languages such as python, java, ruby, js... See wiki

    A call by reference behaviour would indeed make foo! to change the zeros to ones. However Julia doesn't support that. (If you know C#, that is what ref or out does)

    0 讨论(0)
  • 2021-02-05 09:27

    In practice, regardless of theory(call by sharing), described in previous answer, everything happens in Julia as if pointer variables as arrays were passed by reference, while scalar variables, such as numbers, were passed by value.

    This is a pain for people like me, used to C or Pascal languages, where one specify in formal parameter part in function declaration, if the parameter is by value or by reference.

    However, due to Julia's feature that allows return of multiple values in a function, there is an elegant way to simulate parameters by reference for scalar variables. Obviously this works even for immutable variables like strings, since the variables are recreated.

    Julia's Code

    function process(a,b,c)
      a += c 
      b *= c
      return a*b*c, a, b  # normal result, changing "a" and "b"
    end
    
    a = 4
    b = 7
    println("Before: ", a," ",b)
    result, a, b = process(a,b,7)
    println("After: ", a," ",b," ", result)
    

    Display

    Before: 4 7
    After: 11 49 3773 
    

    a and b was both changed inside function process. a was added to 7 and b was multiplied by 7

    0 讨论(0)
提交回复
热议问题