Actually it's a fundamental principle in Mathematica that functions shouldn't change their arguments. Instead, they return modified data:.
For example, this is a list:
In[1]:= a = {1, 2, 3}
Out[1]= {1, 2, 3}
This returns a new list which has 4
appended at the end ...
In[2]:= Append[a, 4]
Out[2]= {1, 2, 3, 4}
... but it does not change a
:
In[3]:= a
Out[3]= {1, 2, 3}
The functions that do actually change variables are a tiny minority, and include =
and :=
(obviously); +=
, *=
and similar ones such as ++
, --
; and finally AppendTo
and PrependTo
.
Regarding discoverability, once you get used to how Mathematica works, this behaviour will feel natural and you'll realize that it makes functions behave predictably. Some other languages have both variety, functions like Append
and functions like AppendTo
, for many tasks, and you'll always have to check the documentation to see what a given function does precisely.
Mathematica does not have this problem. Does, e.g., Sort[a]
change the order of the elements in a
? No, it doesn't, it just returns a new list with elements rearranged.
Some more reading:
* http://mathematica.stackexchange.com/a/42270/12
If you are considering appending multiple times in a loop, check Sow
and Reap
instead.