# How to modify value of \*scalar\* argument passed to function so that it persists in calling function?

**URL:** <https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151>\
**Category:** New to Julia\
**Created:** [August 4, 2019, 4:14pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151 "2019-08-04T16:14:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafal\_Machalica](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafal_machalica/32/8207_2.png) [@Rafal\_Machalica](https://discourse.julialang.org/u/Rafal_Machalica)\
**Post date:** [August 4, 2019, 4:14pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/1 "2019-08-04T16:14:27Z")

</div>

I want to obtain standard C behaviour of following minimal example:

```julia
int a = 5;
void f (int *a)
{
    *a = *a+1;
}
f(&a);

```

after calling the function, variable “a” will still be equal 6 in outer scope.

I am aware that in Julia, functionally, I can obtain it by

```julia
a::Int = 5
function f(a)
    return a+1
end
a = f(a)

```

but I believe it is less efficient due to unnecessary memory allocation and assignments.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 4, 2019, 4:32pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/2 "2019-08-04T16:32:13Z")

</div>

> [@Rafal\_Machalica](#):
>
> but I believe it is less efficient due to unnecessary memory allocation and assignments.

No, the heap will not be used here in Julia. You don’t even need that `::Int` for that to optimize.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [August 4, 2019, 4:48pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/3 "2019-08-04T16:48:29Z")

</div>

What you probably want is to wrap the value in an `Array` or in a mutable struct.

---

<div class="post-metadata">

**Author:** ![Rafal\_Machalica](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafal_machalica/32/8207_2.png) [@Rafal\_Machalica](https://discourse.julialang.org/u/Rafal_Machalica)\
**Post date:** [August 4, 2019, 5:11pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/4 "2019-08-04T17:11:27Z")

</div>

Well, 1x1 array or one-field struct are not as elegant as plain variable. I am aware of these workarounds, bot still looking for non-awkward solution.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [August 4, 2019, 5:23pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/5 "2019-08-04T17:23:35Z")

</div>

An elegant, idiomatic way to do this is to wrap the value you want to make mutable in a `Ref` (like `a = Ref{Int}(5)`, and then mutate the value within your function like so: `a[] += 1`. This is actually pretty much how passing object pointers to `ccall` is done internally by lowering, as wrapping an immutable value with `Ref` forces that value to take on a fixed address in memory for the duration of the `Ref`’s lifetime.

EDIT: Full example:

```julia
a = Ref{Int}(5)
function f(a)
    a[] += 1
end
f(a)

```

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [August 4, 2019, 5:29pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/6 "2019-08-04T17:29:00Z")

</div>

Note that the `mutable struct` approach suggested is possibly even more idiomatic than my suggested `Ref` approach if you plan to dispatch on the type of the value.

---

<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:** [August 4, 2019, 5:32pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/7 "2019-08-04T17:32:33Z")

</div>

But the poster is asking for this for performance reasons. Is there any reason to believe that it will be better for performance? As far as I understand it will _not_, so it’s just a bad idea all around.

A benchmark test

```julia
julia> foo(a) = a + 1
foo (generic function with 2 methods)

julia> bar(a) = a[] += 1
bar (generic function with 1 method)

julia> @btime foo(b) setup=(b=5)
  0.022 ns (0 allocations: 0 bytes)
6

julia> @btime bar(a) setup=(a=Ref{Int}(5))
  1.662 ns (0 allocations: 0 bytes)
6

```

Preliminary conclusion: Don’t do it, @Rafal_Machalica, it’s a bad idea. (I guess the benchmark isn’t really working here, but you should really use immutables as much as you can, it’s better programming practice, and probably more efficient.)

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [August 4, 2019, 5:41pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/8 "2019-08-04T17:41:09Z")

</div>

Sure, for performance reasons passing and returning values (instead of manually boxing them) is going to be more efficient. I was just responding directly to the title of the post 🙂

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [August 4, 2019, 11:31pm UTC](https://discourse.julialang.org/t/how-to-modify-value-of-scalar-argument-passed-to-function-so-that-it-persists-in-calling-function/27151/9 "2019-08-04T23:31:59Z")

</div>

Just a minor comment: this is not a matter of being scalar or not, but of the type being mutable or immutable: mutable structs can be mutated by a function, immutable structs cannot. Julia uses “[call by sharing](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing)” evaluation strategy, like Python to name one. That’s why above you’ve been suggested to wrap the object in a mutable struct. You’ve been told why boxing your immutable object in a mutable struct is a bad idea for performance. Just make sure that your function `f` [doesn’t change the type](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Avoid-changing-the-type-of-a-variable-1) of your object `a`, otherwise that could be another performance trap.
