# Calling Clear within a function [duplicate]

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.

• 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) – Mr.Wizard Nov 12 '16 at 14:44
• Are you expecting anything to happen to a when the internal x is cleared? – Edmund Nov 12 '16 at 14:44
• @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. – Mr.Wizard Nov 12 '16 at 14:46
• a = 1; SetAttributes[f, HoldAll]; f[a_] := Clear[a]; – bill s Nov 12 '16 at 14:50
• My first reaction is: you probably don't really want to do this. Can you explain why you want to proceed this way? – Alan Nov 12 '16 at 16:09