# Pass by reference problem

**URL:** <https://discourse.julialang.org/t/pass-by-reference-problem/7329>\
**Category:** General Usage\
**Created:** [November 27, 2017, 7:46am UTC](https://discourse.julialang.org/t/pass-by-reference-problem/7329 "2017-11-27T07:46:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 27, 2017, 7:46am UTC](https://discourse.julialang.org/t/pass-by-reference-problem/7329/1 "2017-11-27T07:46:47Z")

</div>

```julia
julia> type RR
           x::Int
       end

julia> function tt(x,y)
           if (x+=y) > 0
               x
           else
               y
           end
       end
tt (generic function with 1 method)

julia> import Base:+,>,<

julia> +(a::RR,b::RR) = RR(a.x+b.x)
+ (generic function with 181 methods)

julia> >(a::RR,b::Int) = a.x > b
> (generic function with 3 methods)

julia> <(a::Int,b::RR) = a<b.x
< (generic function with 60 methods)

julia> x = RR(3)
RR(3)

julia> y = RR(5)
RR(5)

julia> tt(x,y)
RR(8)

julia> x
RR(3)

```

even though the returning result is RR(8) but the passed argument x is not changed? Why is that?  
Thanks

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 27, 2017, 8:02am UTC](https://discourse.julialang.org/t/pass-by-reference-problem/7329/2 "2017-11-27T08:02:10Z")

</div>

```julia
+(a::RR,b::RR) = RR(a.x+b.x)

```

constructs a new instance. You’d have to do `+(a::RR,b::RR) = a.x+=b.x` but I strongly suggest that you don’t do that as it is not what `+` is supposed to do. Also, as your example stands, you don’t gain anything from in-place modification versus just making `RR` a immutable type and doing it normally.

If your type would be more of a container, you’d do it with the `broadcast` machinery.

---

<div class="post-metadata">

**Author:** ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)\
**Post date:** [November 27, 2017, 8:11am UTC](https://discourse.julialang.org/t/pass-by-reference-problem/7329/3 "2017-11-27T08:11:07Z")

</div>

So basically assignment works as an identifier points to something. Not a reference, for a reference you need to access it fields.

What about broadcast ? Can you give me an example?

Thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 27, 2017, 8:32am UTC](https://discourse.julialang.org/t/pass-by-reference-problem/7329/4 "2017-11-27T08:32:26Z")

</div>

Broadcasting changed [recently with #23939](https://github.com/JuliaLang/julia/pull/23939/files), which has a nice writeup. You might want to look at that. But note that is the development version. I would wait for that to trickle into stable though, it became much less complex than previously.

Also, use `struct` instead of `type`, which is previous syntax and will be removed. It has the extra advantage of being immutable by default (unless you ask for `mutable struct`), so it encourages the kind of approach that @mauro3 suggested.
