# How to define a function such that its arguments can mutate?

**URL:** https://discourse.julialang.org/t/how-to-define-a-function-such-that-its-arguments-can-mutate/66320
**Category:** General Usage
**Created:** [August 13, 2021, 4:59am UTC](https://discourse.julialang.org/t/how-to-define-a-function-such-that-its-arguments-can-mutate/66320 "2021-08-13T04:59:34Z")
**Posts on this page:** 1
**Showing post:** 12

<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 13, 2021, 7:44am UTC](https://discourse.julialang.org/t/how-to-define-a-function-such-that-its-arguments-can-mutate/66320/12 "2021-08-13T07:44:24Z")

</div>

I think the idiomatic style is as follows:

- A mutable `A` is modified by a function as `f!(A)`.
- An immutable `x` is modified by a function usually as `x = f(x)`.

Example:

```julia
function f!(A)
    A .= 1:length(A)
end

A = zeros(4)
f!(A)
@show A;

```

```julia
A = [1.0, 2.0, 3.0, 4.0]

```

```julia
f(x) = x + 1

x = 99
x = f(x)
@show x;

```

```julia
x = 100

```

---

_[View the full topic](https://discourse.julialang.org/t/how-to-define-a-function-such-that-its-arguments-can-mutate/66320)._
