0
$\begingroup$

I want to create a function that when it is called clears the value of a parameter.

For example I want to keep the value of a parameter and then clear its valueby calling a function:

a = 1;
f[x_] :=
 {b = x;
Clear[x]}
f[a]

However calling f[a] gives an error Clear::ssym: 1 is not a symbol or a string as expected. How can I overcome this problem.

$\endgroup$
  • 2
    $\begingroup$ You will need to add a Hold Attribute to f. See mathematica.stackexchange.com/a/18737/121 and mathematica.stackexchange.com/q/17767/121. (I favor marking this question as a duplicate) $\endgroup$ – Mr.Wizard Nov 12 '16 at 14:44
  • $\begingroup$ Are you expecting anything to happen to a when the internal x is cleared? $\endgroup$ – Edmund Nov 12 '16 at 14:44
  • $\begingroup$ @Edmund From one perspective there is no internal x, as there would be with Module; at the time of evaluation it has already been replaced with 1. I suppose you know this but it might still confuse new users. $\endgroup$ – Mr.Wizard Nov 12 '16 at 14:46
  • $\begingroup$ a = 1; SetAttributes[f, HoldAll]; f[a_] := Clear[a]; $\endgroup$ – bill s Nov 12 '16 at 14:50
  • 2
    $\begingroup$ My first reaction is: you probably don't really want to do this. Can you explain why you want to proceed this way? $\endgroup$ – Alan Nov 12 '16 at 16:09

Browse other questions tagged or ask your own question.