# Is there any way to explicitly prohibit a function from modifying the \`mutable struct\` or \`Vector/Array\` parameters?

**URL:** https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416
**Category:** General Usage
**Tags:** question
**Created:** [August 15, 2021, 2:40am UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416 "2021-08-15T02:40:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Pingzi\_Kang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pingzi_kang/32/14318_2.png) [@Pingzi\_Kang](https://discourse.julialang.org/u/Pingzi_Kang)
#### Post date: [August 15, 2021, 2:40am UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416/1 "2021-08-15T02:40:21Z")

</div>

As we know, `Julia`’s functions can modify the contents of the passed `mutable struct` or `Array`. Sometimes this can cause confusion as to whether the function modifies the members of the `mutable struct` or not. It might be helpful to write a detailed document.

I would like to ask if there are other solutions?

Thanks!

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [August 15, 2021, 3:13am UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416/2 "2021-08-15T03:13:55Z")

</div>

[https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention](https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention)

---

<div class="post-metadata">

### Author: ![Pingzi\_Kang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pingzi_kang/32/14318_2.png) [@Pingzi\_Kang](https://discourse.julialang.org/u/Pingzi_Kang)
#### Post date: [August 15, 2021, 4:48am UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416/3 "2021-08-15T04:48:16Z")

</div>

Thanks for your reply!

[Append `!` to names of functions that modify their arguments](https://docs.julialang.org/en/v1/manual/style-guide/#bang-convention), this is indeed sufficient in everyday situations. But in complex situations, it may not help much, e.g.

```julia
mutable struct TypeA
    #
end

mutable struct TypeB
    #
end

function fun!(a::TypeA, b::TypeB, c::Vector{Float64})
    #
end

```

I may only modify `a`, or I may modify both `a` and `b` at the same time (even modify `c`).

I think just adding a symbol `!` doesn’t help much.

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [August 15, 2021, 4:55am UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416/4 "2021-08-15T04:55:49Z")

</div>

Julia users who know the meaning of `!` are savvy enough to browse the code to see which arg is mutated and which is not.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [August 15, 2021, 7:17am UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416/5 "2021-08-15T07:17:48Z")

</div>

Before that, I would probably read the documentation of the function.

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 15, 2021, 1:56pm UTC](https://discourse.julialang.org/t/is-there-any-way-to-explicitly-prohibit-a-function-from-modifying-the-mutable-struct-or-vector-array-parameters/66416/6 "2021-08-15T13:56:57Z")

</div>

> [@Pingzi\_Kang](#):
>
> I think just adding a symbol `!` doesn’t help much.

You can help yourself much by adding a doc string.

Minimal working example:

```julia
mutable struct TypeA
    n::Int
end

mutable struct TypeB
    x::Float64
end

"""
`fun!(a::TypeA, b::TypeB, c::AbstractVector{Float64})` only mutates `b`.
"""
function fun!(a::TypeA, b::TypeB, c::AbstractVector{Float64})
    b.x = sum(c) / a.n
end

a, b, c = TypeA(10), TypeB(0.0), collect(1.0:10.0)
fun!(a, b, c)
@show a b c;

```

```julia
a = TypeA(10)
b = TypeB(5.5)
c = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]

```

```julia
@doc fun!

```

```julia
  fun!(a::TypeA, b::TypeB, c::AbstractVector{Float64}) only mutates b.

```

```julia
julia> ?
help?> fun!

```

```julia
search: fun! function Function functionloc @functionloc @cfunction fullname

  fun!(a::TypeA, b::TypeB, c::AbstractVector{Float64}) only mutates b.

```
