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

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:

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

A = zeros(4)
f!(A)
@show A;
A = [1.0, 2.0, 3.0, 4.0]
f(x) = x + 1

x = 99
x = f(x)
@show x;
x = 100
4 Likes