# An Issue in Defining a Mutating Function

**URL:** <https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933>\
**Category:** General Usage\
**Tags:** question, function, functions\
**Created:** [March 11, 2023, 7:54pm UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933 "2023-03-11T19:54:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [March 11, 2023, 7:54pm UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/1 "2023-03-11T19:54:41Z")

</div>

This might sound a stupid question to experienced Julia users. When I define a simple mutating function as:

```julia
function foo!(x)
    x = x + 1
end

```

I expect the function to change the value of its argument. However when I execute the following:

```julia
x = 3
foo!(x)
x

```

the value of x is still 3. What’s going on here?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [March 11, 2023, 9:09pm UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/2 "2023-03-11T21:09:30Z")

</div>

two things.

1. `Int`s are immutable objects, so you can’t mutate them in any way
2. Even if `x` _was_ mutable (like an array), your method would not mutate it. All you are doing is assigning a new binding inside the function. The outer scope doesn’t know about that.

You might want to use a vector here.

```julia
function foo!(x)
    x[1] = x[1] + 1
end

x = [3]
foo!(x)

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 11, 2023, 10:25pm UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/3 "2023-03-11T22:25:26Z")

</div>

> [@pdeffebach](#):
>
> You might want to use a vector here.

`Ref` is also a good option (and has slightly better performance):

```julia
julia> foo!(x) = x[] += 1
foo! (generic function with 1 method)

julia> x = Ref(3)
Base.RefValue{Int64}(3)

julia> foo!(x)
4

julia> x
Base.RefValue{Int64}(4)

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [March 11, 2023, 10:37pm UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/4 "2023-03-11T22:37:45Z")

</div>

If you want to mutate a global variable in a function, you should declare it to be `global`, or else all variables in your function will be treated as local variables. This works for your example:

```julia
julia> function foo!(x)
            global x += 1
        end
foo! (generic function with 1 method)

julia> x = 3
3

julia> foo!(x)
4

julia> x
4

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 11, 2023, 10:45pm UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/5 "2023-03-11T22:45:32Z")

</div>

Note that this violates the expectation of `!` to mean that the function will mutate its argument:

```julia
julia> function foo!(x)
           global x += 1
       end
foo! (generic function with 1 method)

julia> x = 3
       foo!(x)
4

julia> y = 10
       foo!(y)
5

julia> y
10

```

---

<div class="post-metadata">

**Author:** ![Andrew\_Swann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrew_swann/32/32106_2.png) [@Andrew\_Swann](https://discourse.julialang.org/u/Andrew_Swann)\
**Post date:** [March 12, 2023, 9:18am UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/7 "2023-03-12T09:18:52Z")

</div>

So would

```julia
julia> x::Int64 = 1
1

julia> function foo()
         global x += 1
       end
foo (generic function with 1 method)

julia> foo()
2

julia> foo()
3

julia> x
3

```

be a more standard way to do this?

---

<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:** [March 12, 2023, 9:33am UTC](https://discourse.julialang.org/t/an-issue-in-defining-a-mutating-function/95933/8 "2023-03-12T09:33:45Z")

</div>

This is not mutation either. That’s equivalent to

```julia
x = 1
x = 2

```

Which is not mutating, is assigning the label `x` to a new value.

Mutability is a property of the value, not of the label. And an Int is not mutable.

Concerning the original question, probably what the OP wants is

```julia
x = 1
f(x) = x + 1
# call reassigning x to the return value 
x = f(x)

```

(Although technically correct, I don’t think suggesting to use arrays or refs is actually what will solve the problem, and may lead to very bad initial programming habits and experience with Julia; this may help: [Assignment and mutation · JuliaNotes.jl](https://m3g.github.io/JuliaNotes.jl/stable/assignment/)).
