# How to update scalar and Boolean in-place?

**URL:** <https://discourse.julialang.org/t/how-to-update-scalar-and-boolean-in-place/84843>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 26, 2022, 7:30pm UTC](https://discourse.julialang.org/t/how-to-update-scalar-and-boolean-in-place/84843 "2022-07-26T19:30:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 26, 2022, 7:30pm UTC](https://discourse.julialang.org/t/how-to-update-scalar-and-boolean-in-place/84843/1 "2022-07-26T19:30:20Z")

</div>

I have function `update2` which returns the values of `status, nChange`. However, I need to build function `update1` which update `status, nChange` without returning their values (in-place mutation) but it seems not correct process. How can I implement that correctly?

```julia

using SparseArrays, LinearAlgebra

function update1(status, nChange, check)
       if check == 1
              status = true;
              nChange += 1;
       end
end

function update2(status, nChange, check)
       if check == 1
              status = true;
              nChange += 1;
       end
       status, nChange;
end

function run1()
       status = false;
       nChange = 0;
       check = 1;
       update1(status, nChange, check)
       status, nChange;
end

function run2()
       status = false;
       nChange = 0;
       check = 1;
       status, nChange = update2(status, nChange, check)
       status, nChange;
end

status1, nChange1 = run1()
status2, nChange2 = run2()

julia> status1
false

julia> nChange1
0

julia> status2
true

julia> nChange2
1

```

---

<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:** [July 26, 2022, 8:02pm UTC](https://discourse.julialang.org/t/how-to-update-scalar-and-boolean-in-place/84843/2 "2022-07-26T20:02:49Z")

</div>

Use a `Ref()`

```julia
julia> function update!(x)
           x[] += 1
       end;

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

julia> update!(y);

julia> y
Base.RefValue{Int64}(2)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 26, 2022, 10:37pm UTC](https://discourse.julialang.org/t/how-to-update-scalar-and-boolean-in-place/84843/3 "2022-07-26T22:37:22Z")

</div>

`Int`s and `Bool`s, and all other `Number`s, and many, many other types are `immutable`, so you cannot update them.

Updating objects of _mutable_ type is possible, though, so wrapping your value in a mutable type like `Ref` will work.

But even if you have a mutable object, doing this

> [@Amro](#):
>
> ```julia
> status = true;
> nChange += 1;
> 
> ```

will not mutate them, the above only _rebinds_ the variables, and it will not be visible outside the function.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 27, 2022, 1:41pm UTC](https://discourse.julialang.org/t/how-to-update-scalar-and-boolean-in-place/84843/4 "2022-07-27T13:41:46Z")

</div>

Thank you very much I got it!
