# Make function which changes varriable

**URL:** <https://discourse.julialang.org/t/make-function-which-changes-varriable/48423>\
**Category:** New to Julia\
**Created:** [October 15, 2020, 11:27am UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423 "2020-10-15T11:27:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 15, 2020, 11:27am UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/1 "2020-10-15T11:27:49Z")

</div>

The example is pretty self-explanatory, I want to make a function which changes a variable. But it doesn’t work, how do I make it to?

```julia
a = 2
function change_a!(a)
    a = 3
    nothing
end
change_a!(a)
display(a);

```

the output is `2`.  
(I wish it was 3)

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 15, 2020, 11:31am UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/2 "2020-10-15T11:31:09Z")

</div>

The keyword you’re looking for is `global a`, but this isn’t recommended (see: [Avoid global variables](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-global-variables)). Using global variables often leads to confusing behaviour, bugs, performance problems and a plethora of other nastiness. Instead, simply return the value and assign it to a variable.

Note that containers (such as Vectors, Matrices) _can_ be modified. This is because in that case you’re not modifying the outer variable itself, but the content that variable is wrapping.

If you’re coming from a different programming language, you might find [Noteworthy differences from other programming languages](https://docs.julialang.org/en/v1/manual/noteworthy-differences/) helpful.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 15, 2020, 11:51am UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/3 "2020-10-15T11:51:25Z")

</div>

Or maybe, this is what you are looking for:

```julia
julia> a_ref=Ref(2)
Base.RefValue{Int64}(2)

julia> a_ref[]
2

julia> function self(ref_a)
       ref_a[]=4
       end
self (generic function with 1 method)

julia> self(a_ref)
4

julia> a_ref[]
4

```

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 15, 2020, 11:52am UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/4 "2020-10-15T11:52:31Z")

</div>

I understand, thanks.  
So this is appropriate code:

```julia
a = [2]
function change_a!(a)
    a[1] = 3
    nothing
end
change_a!(a)
display(a[1]);

```

is it, however, appropriate (since it was inappropriate to make `a` global?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 15, 2020, 12:01pm UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/5 "2020-10-15T12:01:14Z")

</div>

This is better, since in `change_a!` you’re modifying the contents of the argument, not the global variable directly.

Note that `global a` would be used in `change_a!`, to tell the function to look in the containing scope for `a` instead of in the local scope. In both your original example and your second one, `a` is a global variable and also a local variable. See [Scoping](https://docs.julialang.org/en/v1/manual/variables-and-scoping/).

I’ll annotate global and localness here for you:

```julia
a = [2] # Declaring the variable `a` to point to an Array{Int,1} containing `2`
function change_a!(a) # change_a! has one argument named a
    a[1] = 3 # local a, set the first index equal to 3
    nothing
end
change_a!(a) # call change_a! with the global a
display(a[1]); # get the first index of the global a and display it

```

Still, the best approach would be having some sort of `main` or `setup` function that takes care of initialization of your variables and passes them to your inner work:

```julia
function change_element!(a)
    a[1] = 3
end

function main()
    a = [2]
    change_element!(a)
    display(a[1])
end

```

You would then call `main()` from the REPL.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [October 15, 2020, 12:03pm UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/6 "2020-10-15T12:03:36Z")

</div>

Thanks for the explanation. Also quite impressed by @oheil and whatever magic that is.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [October 15, 2020, 12:06pm UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/7 "2020-10-15T12:06:15Z")

</div>

No magic at all:  
[https://docs.julialang.org/en/v1/base/c/#Core.Ref](https://docs.julialang.org/en/v1/base/c/#Core.Ref)  
It’s part of Julia’s interface calling to C.

A good read I found about the topic:

> **[Working with and emulating References in Julia](https://erik-engheim.medium.com/working-with-and-emulating-references-in-julia-e02c1cae5826)**
>
> Using Ref, Array and Tuple types for reference like behavior

@Sukera 's array solution also mentioned and explained there.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 15, 2020, 12:08pm UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/8 "2020-10-15T12:08:44Z")

</div>

If you want to go the `Ref` or `Array` route, you’ll want to mark them as `const` (i.e., `const a = Ref(2)`) to somewhat avoid the performance cost of a maybe changing type (that’s the problem with globals). You will still have some overhead though because you have to dereference them - there’s no shortcut here. You might also have race conditions if you multithread your code, which is why it’s easiest and best to avoid them from the beginning.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 15, 2020, 12:40pm UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/9 "2020-10-15T12:40:00Z")

</div>

Perhaps it is worth mentinoning that:

1. Scalars cannot be changed by default. They are immutable values. You can change, however, the value associated with the label `a`, and to do so you you better return the value from your function:

```julia
function foo(a)
    a = a + 1
    return a
end

```

and then call this function with

```julia
a = foo(a)

```

From a user perspective, this will change the value associated to `a` in the scope where the function was called from. This does not mean that the “previous `a`” has changed, but the value associated to the label `a` is another one now.

1. Arrays can be changed, they are mutable. And that is why your example with `a=[2]` works. This is fine to do, but generally it is thought to be used for larger arrays, because there is where “changing” the values in the same position in memory is worth relative to just copying the values.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 15, 2020, 1:58pm UTC](https://discourse.julialang.org/t/make-function-which-changes-varriable/48423/10 "2020-10-15T13:58:06Z")

</div>

Again late, but let shamelessly point out [my post about how you should not see variables as boxes](https://discourse.julialang.org/t/is-it-appropriate-to-say-a-variable-is-a-label-or-box/46068/19) to complement what Leandro is saying.
