Is it valid to do in place operations on multiple arguments? I’m interested in writing a function of the form:
function f!(x,y,p)
# commands which change x and y
x,y
end
where x
and y
are modified and p
is some set of additional parameters.
Yes, appending !
is just a convention to signal that the function modifies some of its arguments, not which one(s). I would suggest that you add a docstring and/or comment that describes which arguments the function modifies.
1 Like
There was also some discussion of signaling the intent of mutating arguments by writing
function f!(x!,y!,p)
# commands which change x! and y!
x!,y!
end
This makes it abundantly clear which variables are mutated, even if you don’t write proper documentation: Both the help, via help?> f!
, and methods(f!)
show the formal argument names. Julia programmers who have never seen this before will instantly understand what you are trying to communicate with this.
Disadvantages: May look ugly (matter of taste). If used very inconsistently, people might mistakenly assume that arguments without exclamation marks are not mutated. Not widely used as a convention, and fracturing the coding style is never good for an ecosystem.
1 Like