# Function argument not being updated correctly

**URL:** <https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105>\
**Category:** New to Julia\
**Created:** [April 6, 2022, 3:24pm UTC](https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105 "2022-04-06T15:24:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![staufenbach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/staufenbach/32/35221_2.png) [@staufenbach](https://discourse.julialang.org/u/staufenbach)\
**Post date:** [April 6, 2022, 3:24pm UTC](https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105/1 "2022-04-06T15:24:49Z")

</div>

hi,

i have a mutable struct representing an octree. its contains various info such as the list of all particle (another struct) inside it. I passed it as an argument of a function that should modify it. but only some part of the update are reflect in the main scope.

```julia
function verlet!(_tree)
    for part in _tree.points
        part.u += part.acc_x * dt/2
        part.v += part.acc_y * dt/2
        part.w += part.acc_z * dt/2

        part.x += part.u * dt
        part.y += part.v * dt
        part.z += part.w * dt

        # reset acc
        part.acc_x = 0
        part.acc_y = 0
        part.acc_z = 0
    end
    
    tmp = _tree.points
    _tree = nothing
    
    _tree = tree(1, _range)

    for p in tmp
       Tree.insert!(p, _tree)
    end

    for part in _tree.points
        query_acc!(_tree, part, 0.5, 10e16)
    end

    for (i, part) in enumerate(_tree.points)    
        part.u += part.acc_x * dt/2
        part.v += part.acc_y * dt/2
        part.w += part.acc_z * dt/2
    end

end

```

the problem is that in the main scope only the position and velocity of the particle are visibly changed, while the rest seems untouched.  
the acceleration seems to be updated locally as the velocity are updated correctly but in the main scope its still equals 0. calling query\_acc! in the repl change the acceleration value correctly.  
same the global struct of the tree dont change. if i suppress the insert! part, the tree should be empty but in the main scope its still intact.

ps: i hope in clear enough.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [April 6, 2022, 3:34pm UTC](https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105/2 "2022-04-06T15:34:37Z")

</div>

These lines:

> [@staufenbach](#):
>
> ```julia
> tmp = _tree.points
> _tree = nothing
>     
> _tree = tree(1, _range)
> 
> for p in tmp
> Tree.insert!(p, _tree)
> end
> 
> ```

Re-bind the name `_tree` to a new value and initialize that value. After this point, the function has no way to modify the original value. Does it work correctly if you delete these lines?

---

<div class="post-metadata">

**Author:** ![staufenbach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/staufenbach/32/35221_2.png) [@staufenbach](https://discourse.julialang.org/u/staufenbach)\
**Post date:** [April 6, 2022, 3:47pm UTC](https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105/3 "2022-04-06T15:47:31Z")

</div>

It works correctly after your correction.

i just have to find a way to recompute/update the tree. now ! splitting the function in 2 would do the trick i guess but i would like to keep the current structure of the code.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 6, 2022, 6:03pm UTC](https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105/4 "2022-04-06T18:03:05Z")

</div>

Return the new tree, and call it as:

```julia
tree = verlet!(tree)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 6, 2022, 6:08pm UTC](https://discourse.julialang.org/t/function-argument-not-being-updated-correctly/79105/5 "2022-04-06T18:08:36Z")

</div>

> [@staufenbach](#):
>
> ```julia
> _tree = nothing
> _tree = tree(1, _range)
> 
> ```

Better yet, instead of these lines you should mutate the fields of `_tree` directly.

You need to avoid any statement like `_tree = something` if you want the changes to be visible to the caller. Only do things like `_tree.something = something`

I think the key issue here is that you are confusing mutation (what you want to do) with assignment (`_tree = ...`). See e.g. [Assignment and mutation · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/assignment/)
