## Clear

Before we can really begin working with Mathematica we also need to discuss the function Clear , which can be a life-saver.

It just removes any definitions we've given to a symbol, which can be a bigger deal than one might think. Consider the following case:

 aVariableIUsedBefore=1000;

aVariableIUsedBefore[x_]:=x*10;

SetDelayed::write: Tag Integer in 1000[x_] is Protected.

Notice we get an error because the way Mathematica reads this, we're trying to assign a function to the number 1000.

Here, though, all we need to do is use Clear and we're good to go.

 Clear[aVariableIUsedBefore]


Sometimes we've made a lot of definitions and we want to Clear them all, just to prevent hard to find errors from cropping up. In this case we can do the following:

 Clear["Global*"]


What this does is removes all the definitions for any symbol that looks like GlobalsymbolName which is usually every symbol we've defined.

Sometimes we want to protect some symbols, like fundamental constants, from being erased. In this case we just need to make a symbol that doesn't look like GlobalsymbolName . A useful way to do this is to put the type of thing it is before it:


ConstantPlanckConstant = 6.626*10-34 (*J*s*);



This sort of thing is discussed more at length in the Contexts section, but first let's just see that this does what we want it to. First let's define a bunch of things:

 a=100;
b=2.50;
c[x_]=RandomReal[]*x;
x[x_]:=x[x];

a
b
c[10]
x[10]

 100

 2.5

 9.548010769916973

 10[10]


And now we'll clear them:

 Clear["Global*"]

a
b
c[10]
x[10]

 a

 b

 c[10]

 x[10]


But our constant is fine:

 ConstantPlanckConstant

 6.6260000000000015*^-34


And for those who don't want to type out a long name every time they use this constant in a problem, here's a trick: use some variable just for the problem to which you assign the value of the constant. e.g:


ConstantSpeedOfLight=3*108(*m/s*);


 h=ConstantPlanckConstant;
c=ConstantSpeedOfLight;
λ=1*10^-3 (*m*);

h*c/λ (*J*)

 1.9878000000000004*^-22


Then just clear all of this before starting the next problem:

 Clear["Global*"]


And now none of that can leak into the next problem:

 h
c
λ

 h

 c

 
λ