# Mutable function parameter of primitive type

**URL:** https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204
**Category:** General Usage
**Created:** [May 26, 2020, 3:26pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204 "2020-05-26T15:26:52Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [May 26, 2020, 3:26pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/1 "2020-05-26T15:26:52Z")

</div>

I’m going through Julia manual and I’m having a bit of a problem comprehending mutability and pass by sharing concept as applied to primitive types. Can some show me an example where `x` will be modified by calling `f(x)` so the value printed is not `42`:

```julia
x = 42
function f(x) ....... end
println(x)

```

Let’s put aside the bigger question of sound software engineering practices. I just want to see it done. BTW, I know that wrapping `x` in a `mutable struct` would do the trick, but is this the only way?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 26, 2020, 3:44pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/2 "2020-05-26T15:44:38Z")

</div>

No, primitive types like `Int` are immutable. You can only change their binding, not mutate their value.

As you alluded, the way to do this is wrapping in a mutable type, usually `Ref(x)` or whatever.

You can use the `isimmutable` function to see if a value may be mutated or not:

```julia
julia> isimmutable(42)
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: [May 26, 2020, 4:00pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/3 "2020-05-26T16:00:23Z")

</div>

Note that rather than passing a number as a mutable parameter (e.g. wrapped in a `Ref`), it’s generally much better to simply return the updated value. You can return multiple values with a tuple.

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [May 26, 2020, 4:02pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/4 "2020-05-26T16:02:20Z")

</div>

> [@stevengj](#):
>
> Note that rather than passing a number as a mutable parameter (e.g. wrapped in a `Ref` ), it’s generally much better to simply return the updated value. You can return multiple values with a tuple.

Of course, but see my disclaimer about sound software engineering practices. 😀

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [May 26, 2020, 4:07pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/5 "2020-05-26T16:07:09Z")

</div>

I would not contemplate to check `42` for immutability in my exercise, I would check `x`. When I do so, it doesn’t make much sense on the surface for someone coming from C++ world:

```julia
julia> x=0
0
julia> isimmutable(x)
true
julia> x=42
42
julia> x
42

```

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [May 26, 2020, 4:12pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/6 "2020-05-26T16:12:20Z")

</div>

The difference is than in Julia, `x = 0` is just saying “bind the value `0` to the variable `x`”. Then, when you later say `x = 42`, it’s again saying “bind the value 42 to the variable `x`”.

No mutation is happening here, all that’s happening is that the value `x` is bound to is changing. This is unlike when you have `v = [1, 2, 3]` and you do `v[1] = 2` or something. That’s real mutation, not a changing of variable bindings.

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [May 26, 2020, 4:20pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/7 "2020-05-26T16:20:02Z")

</div>

> [@Mason](#):
>
> "bind the value `0` to the variable `x` "

This requires a bit of a neural network rearrangement for an old C++ programmer. I will chew on it.

---

<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: [May 26, 2020, 4:43pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/8 "2020-05-26T16:43:03Z")

</div>

Yes, Julia is quite different from C++ in this respect.

In Julia, _values_ can be immutable or mutable. The value 42 is immutable because it a primitive (immutable) type. The tuple `(1, 2)` is likewise immutable. A value of a `mutable struct` `x` is mutable because you can reach into it and modify it via `x.foo = 1`.

Variables in Julia are just labels. Doing `x = 0` attaches the label `x` to the value `0`. Doing `x = 42` attaches that label to a new value. It has no effect on the previous value that that label happened to be attached to.

This is _completely different_ than C++. In C++, doing:

```julia
x = y

```

can have arbitrary side-effects because it calls the `operator=` method of whatever the type of `x` is.

In Julia, doing `x = y` just attaches the label `x` to the value of `y`. It never has side-effects.

---

<div class="post-metadata">

### Author: ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)
#### Post date: [May 26, 2020, 4:53pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/9 "2020-05-26T16:53:40Z")

</div>

The first thing I tried after I got to `mutable struct` in the manual is:

```julia
julia> mutable y=0
ERROR: syntax: extra token "y" after end of expression

```

There seems to be a bit of dichotomy between primitive and composite types in Julia, but I haven’t got to the end of the manual yet. Maybe it will become more clear later.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [May 26, 2020, 5:07pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/10 "2020-05-26T17:07:47Z")

</div>

@pauljurczak maybe it helps to consider a similar example with a mutable value, for example an array?

```julia
x = [0,1]
y = x # x and y both bound to the same value (a mutable array)
x[1] = 3 # change the array
y # the change is also visible from the y binding
x = [5,6] # x now bound to a new value: the array [5,6]
y # y still bound to the first array: [3,1]

```

So `x = ...` is indeed treated the same for mutable and immutable values. This syntax doesn’t change the value, instead it changes the meaning of `x`.

---

<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: [May 26, 2020, 5:58pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/11 "2020-05-26T17:58:24Z")

</div>

> [@pauljurczak](#):
>
> `mutable y`

Right, this doesn’t mean anything in Julia because values are mutable, but variable names are just labels. You can always attach a label to a new value–there is no concept of mutability of the labels themselves.

---

<div class="post-metadata">

### Author: ![enderw88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/enderw88/32/48997_2.png) [@enderw88](https://discourse.julialang.org/u/enderw88)
#### Post date: [April 13, 2023, 10:56pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/12 "2023-04-13T22:56:45Z")

</div>

I was looking for the solution to this problem and it was never actually answered here. Here is an answer that is actually in the [documention](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Passing-Pointers-for-Modifying-Inputs) if you read far enough (which I didn’t and it took forever to figure out)

> ```julia
> width = Ref{Cint}(0)
> range = Ref{Cfloat}(0)
> ccall(:foo, Cvoid, (Ref{Cint}, Ref{Cfloat}), width, range)
> 
> ```
> 
> Upon return, the contents of `width` and `range` can be retrieved (if they were changed by `foo`) by `width[]` and `range[]`; that is, they act like zero-dimensional arrays.

It may not be sound practice, but C functions often modify their parameters to mimic return mulitple values. I my case I was trying to figure out how to use the SDL and this stumped.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [April 14, 2023, 2:43pm UTC](https://discourse.julialang.org/t/mutable-function-parameter-of-primitive-type/40204/13 "2023-04-14T14:43:53Z")

</div>

For a pure-Julia example of mutating scalar inputs:

```julia
function increment!(val, incr)
  val[] += incr # use [] to access the contents of val
  return nothing
end

x = 10
y = Ref(x) # wrap a value in a mutable container
increment!(y, 5)
show(x) # 10 # did not (and can not) mutate the value bound to x
show(y) # Base.RefValue{Int64}(15)
show(y[]) # 15

```

This mutation pattern is frequently used for in-place functions operating on arrays, such as `sort!` or `reverse!`. It’s uncommon in the scalar case (it’s usually much nicer to just return the new value) except when using `ccall` to some foreign code.
