MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Function Name from Inside the function

  • To: mathgroup at
  • Subject: [mg71639] Function Name from Inside the function
  • From: "Philipp" <Philipp.M.O at>
  • Date: Sat, 25 Nov 2006 05:37:20 -0500 (EST)

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, {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.


  • Prev by Date: Re: Method option to Solve
  • Next by Date: Re: AGAIN Nested while loop!!!
  • Previous by thread: Function Name from Inside the Function
  • Next by thread: Re: Function Name from Inside the function