I am fairly new to Mathematica. I am working my way through a number of books. I have been looking at the use of SetDelayed and want to confirm that my understanding is correct. I define a function as follows.
f[x_]:= f[x] = x;
When I evaluate ?f, I can see a rule for this function. Now each time I evaluate f, I see another rule is created for f. So by declaring the function as above, I ensure that for any given value of x the function will only be evaluated once.
Suppose I had declared functions f and g as follows.
f[x_] := f[x] = Sin[x] + Cos[x];
g[x_] := Sin[x] + Cos[x];
Am I correct in saying that, for any given x, f will only be evaluated once? That is, for a given value of x, Sin[x] and Cos[x] will only be called once; whereas in the case of g, each time g is called, Mathematica will evaluate both Sin[x] and Cos[x]? Will using Trace confirm this?
f[x_] := f[x] = (Print[x];x). – Szabolcs Mar 08 '13 at 13:37