I think the simplest way to state my question is to give an example:
x=2;
f[x_]:= x;
(* Is this possible? *)
f[x]
(* x *)
In other words, is it possible to stop Mathematica from evaluating x as 2 before evaluating the function? None of the Hold attributes or functions have successfully halted this part of evaluation for me.
What I'm trying to do is pass symbols to a function for it to manipulate, but I don't want to worry about the same symbol names being used in other parts of the code. Using Clear works, but it then requires me to re-evaluate the areas where the symbol name was used before, so I was hoping there was an easier way other than clearing or using different symbol names.
Edit: Sorry for the confusion. m_goldberg correctly guessed what I was trying to explain. Although I can hide global variables in Module blocks by using the same variable names, I was hoping for the same results with a function. Here is his code that represents my goal:
x = 2;
SetAttributes[f,HoldFirst]
f[x_]:= Module[{u = Defer[x]}, 1 + u + u^2];
f[x]
1 + x + x^2
In other words, I wanted to arbitrarily pick symbols as arguments to a function, regardless if they had been assigned values prior to calling my function. Apparently, I was looking for Defer.
I think it's worth noting that jjc385's answer also achieved my goal, but with a hold on the result.
Thank you everybody for the help!
f
is e.g.HoldFirst
thenx
is not evaluated beforef's
rules are applied. But later, nothing stops it, you can addf[x_]:= Hold[x];
or something. Could you elaborate on what is the goal? $\endgroup$Module
provides...? $\endgroup$