Is there a way to get a function name from inside the function during
the execution time?
One reason is that it would greatly aid debugging.
To illustrate the concept, let's assume that the function names are
stored in the list called $FunctionNames. Now,
In[]:= Clear[f, g, h];
f[x_] := (Print[{Last[#], #} &[$FunctionNames]]; x^2)
g[x_] := (Print[{Last[#], #} &[$FunctionNames]]; f[x] + 5)
h[x_] := Module[{fname = Last[$FunctionNames]},
Print[{Last[#], #} &[$FunctionNames]];
If[MemberQ[debugSymbols, fname],
Print["Debug in ", fname, ", x = ", x]]; g[x]]
In[]:= debugSymbols = {"h"};
h[5]
{h, {h}}
Debug in h, x = 5
{g, {h, g}}
{f, {h, g, f}}
Out[]= 30
(The Print[{Last[#], #} &[$FunctionNames]] statements are only here to
demonstrate the concept; they wouldn't be normally present.)
So, at every function execution a "trace" of functions is available
(recursion would be a pain to handle, though).
Is there anything similar available in Mathematica? Even only the
function name currently executing instead of the "trace" list.
Cheers,
Philipp.