# Correct way of modyfing argument

**URL:** <https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094>\
**Category:** General Usage\
**Created:** [February 15, 2018, 8:32pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094 "2018-02-15T20:32:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [February 15, 2018, 8:32pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/1 "2018-02-15T20:32:10Z")

</div>

Hello, I would like to write a function that is meant to run inside another function (or standalone), that modifies one of the variables of the namespace. An example would be:

```julia
k = 1

function dob(k)
   for i in 1:5
     k *= 2
   end
   return true
end

dob(k) # = 1, I would like it to be 32

```

I understand the behavior and why it doesn’t work. I wanted to ask what is the right way to do it. I could save the value in an object that gets passed as an argument to the function or I could request the global variable.

thanks

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 15, 2018, 8:38pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/2 "2018-02-15T20:38:44Z")

</div>

You can pass a `Ref` (similar to a zero-dimensional array):

```julia
julia> k = Ref(1)
Base.RefValue{Int64}(1)

julia> function dob!(k) # the ! in the name is a convention, indicating an argument is modified
         k[] += 1
       end
dob! (generic function with 1 method)

julia> dob!(k)
2

julia> k[]
2

```

Mutating global variables is (a) slow in Julia and (b) generally considered bad practice because it makes the effects of your functions harder to understand, so I would not suggest using a `global` for this purpose.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [February 15, 2018, 8:47pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/3 "2018-02-15T20:47:20Z")

</div>

> [@bbrunaud](#):
>
> return true

your function returns `true` regardless of the the value of the argument `k`, so you should use `return k`.

```julia
k = 1

function dob(k)
   for i in 1:5
     k *= 2
   end
   return k
end

```

Edit: Perhaps I misunderstood the question.

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [February 15, 2018, 9:05pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/4 "2018-02-15T21:05:58Z")

</div>

Hello, I entered `return true` because my function returns something else that is no exactly what I want to modify. Should I return the value and catch it when running the function?. Is that the proper way?.

---

<div class="post-metadata">

**Author:** ![bbrunaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbrunaud/32/1737_2.png) [@bbrunaud](https://discourse.julialang.org/u/bbrunaud)\
**Post date:** [February 15, 2018, 9:10pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/5 "2018-02-15T21:10:29Z")

</div>

Would it work if k is a vector?, the syntax `k[]` is quite weird.

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [February 15, 2018, 10:17pm UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/6 "2018-02-15T22:17:43Z")

</div>

```julia
function dob(k)
   for i in 1:5
     k *= 2
   end
   return k
end

k=1
k=dob(k)

```

Now `k` has value `32`. This is the preferred pattern for immutable object (like `Int` and any `struct` you define). For a mutable object like a vector you can do:

```julia
function foo!(v)
    v[1]=dob(v[1])
end
v=[1]
foo!(v)

```

Now `v==[32]`. Also you mentioned something about returning something else (not the thing you wanted to modify). In julia it is easy to return two values, eg

```julia
function bar(z)
    dob(z),true
end
z=1
z,success=bar(z)

```

Now `z==32` and `sucess==true`

---

<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:** [February 16, 2018, 12:00am UTC](https://discourse.julialang.org/t/correct-way-of-modyfing-argument/9094/7 "2018-02-16T00:00:22Z")

</div>

You can do this by wrapping your scalar in Ref, but don’t. Don’t do this. Just return the new value from the function, and let the caller assign the result to the old variable (or not). This is cleaner, faster, and more composable.
