2
$\begingroup$

I have a list t1Funcsv0={0.10000000000, 1.00000*10^-7} and I have a function

Maximum[functions_, min_, max_, type_] := (
Which[
Flatten[Position[functions, Max[functions]]], 
Max[functions] == nt,
Flatten[Position[functions, nt]], 
RankedMax[functions, 2] == nt,
Flatten[{Position[functions, Max[functions]],Position[functions,nt]}],
True, 
Print["Error Maximum"]])

to which I pass t1Funcsv0 as well as other arguments. I would like to get the name of the symbol "t1Funcsv0" inside the function Maximum. I tried to use SymbolName[Unevaluated@functions] as I found in Obtaining the name of a variable as a string and Get the name of a symbol passed to a function but it doesn't work because "functions" is already evaluated

$\endgroup$

marked as duplicate by Jason B., m_goldberg, Kuba Sep 14 '18 at 6:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ In the post you linked you see that they give their function the attribute HoldFirst. Have you tried that? $\endgroup$ – Jason B. Sep 13 '18 at 19:45
  • $\begingroup$ Yes I did. If I use Trace I get {t1Funcsv0,{0.10000000000, 1.00000*10^-7}} $\endgroup$ – Rby Sep 13 '18 at 20:24
  • $\begingroup$ I think the answer here is better at and much clearer then the one here Get the name of a symbol passed to a function $\endgroup$ – Rby Sep 13 '18 at 23:39
5
$\begingroup$

You have to use a Hold attribute if you want access to the symbol name during evaluation:

Attributes[myfunc] = HoldFirst;
myfunc[arg1_, arg2_] := (
  Print["The name of the first argument is ", SymbolName[Unevaluated[arg1]]];
  Print["The value of the first argument is ", arg1];
  Print["The max of the first argument is ", Max[arg1]];
  Print["The second argument is ", arg2]
  )

t1Funcsv0 = {0.10000000000, 1.00000*10^-7}
myfunc[t1Funcsv0, bob]

The name of the first argument is t1Funcsv0

The value of the first argument is {0.1,1.*10^-7}

The max of the first argument is 0.1

The second argument is bob

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.