The form like g = x^2
generates OwnValues for g
. The form like f[x_] := x^2
, creates DownValues for the symbol f
. There are other definitions that can get attached to symbols, like SubValues and UpValues. These definitions are saved in a special form, and they are "consulted" in a specific order when expressions are being evaluated.
What is probably the most germane to your question is that DownValues carefully handle the named patterns. So, in f[x_] := x^2
, we can think of x
as a named argument, and so we get x^2
evaluated with whatever was passed in each time we invoke f
. In other words, if I were to evaluate x=5
, then g
would evaluate to 25 every time it was invoked (until x is redefined or g is redefined). Furthermore g /. x -> 2
also evaluates to 25. The first step is to evaluate g
, 25 /. x->2
, then we evaluate x
, 25 /. 5->2
, then the ReplaceAll, 25
(since nothing matches the replacement rule). But on the other hand f[7]
will produce 49--it doesn't get handcuffed to the fact that we evaluated x=5
.
[NOTE: Set versus SetDelayed might be confusing here, but it's not really important. The above is true for both f[x_]:=
or f[x_]=
, assuming x=5
is evaluated afterward. If x had previously been given a value, then the f[x_]:=
form would be the only one that avoids the name collision.]
The answer to your questions,
Are there situations in which using one is more practical, faster or represents any advantage? Do the previous answers change when we have a lot of functions and a lot of variables?
is emphatically "yes". But what choice is best for each situation depends on the situation. It's best to understand Mathematica's evaluation procedure and then match that to your semantics. If I were forced to give a simplistic answer, then DownValues is usually the closest match for function definition.
NIntegrate
with symbollic parameters. $\endgroup$:=
can also lead to issues when mixed with functions that use the type of scoping thatBlock
uses and can be hard to debug for example mathematica.stackexchange.com/a/273480/86543. $\endgroup$