I'm trying to use Modules
together with functions.
tmp2 = x^2 + 1; f[y_] := Module[{x = 1}, Evaluate[y tmp2]]
This works when I just have
tmp2 = x^2 + 1; Module[{x = 1}, Evaluate[tmp2]]
But f[1]
outputs $1 + x^2$.
Edit: After seeing the answers I wonder how I can get something like this to work:
ClearAll[withRules]
SetAttributes[withRules, HoldAll]
withRules[rules_, expr_] :=
Internal`InheritedBlock[{Rule, RuleDelayed},
SetAttributes[{Rule, RuleDelayed}, HoldFirst];
Unevaluated[expr] /. rules]
ClearAll[f, tmp2]
tmp2[y_] = y (x^2 + 1)
withRules[{expr -> tmp2}, f[y_] := Module[{x = 1}, expr[y]]]
Edit 2: Maybe it is better if I explain what I'd like to do.
I have a lot of calculations in terms of $k_x$, $k_y$ and $k_z$. So, those are outputted in terms of these three variables. However, sometimes I need to integrate such expressions but to do this I need to use spherical coordinates. So I set $k_x = k \sin(a) \cos(b)$ and so on and then integrate. I don't want the transformations always set because I want the other calculations to have $k_x$, $k_y$ and $k_z$ as variables, not $a$ en $b$.