7
$\begingroup$

Is there a way to display the variable name instead of its value? for example, I need something likevarname = 1; function[varname]; and the output is varname instead of 1

$\endgroup$
1
  • 2
    $\begingroup$ Have you looked into HoldForm? There are many threads on Holding around, please have a look. $\endgroup$
    – Yves Klett
    Commented Sep 16, 2014 at 13:13

2 Answers 2

2
$\begingroup$
varname = 1;

SetAttributes[ShowName, HoldAll]

ShowName[name_] :=
 Row[{"The name is ", HoldForm @ varname, " and its value is ", ReleaseHold @ varname}]

ShowName @ varname

The name is varname and its value is 1

Or simply

HoldForm @ varname

varname

$\endgroup$
1
  • 4
    $\begingroup$ Why not just say ShowName = HoldForm? $\endgroup$
    – Michael E2
    Commented Sep 16, 2014 at 13:20
7
$\begingroup$

There's also Defer to accomplish this:

varname = 1;

Defer @ varname

varname

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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