Hi John,
Looking at your attached notebook: if you don't know what variables are going to be in expr
, then localising with Block
seems fine to me.
That said... if you're localising particular variables (say, x
and y
), that suggests to me that you do know what variables are going to be in expr
...? If that's the case, I would define it as expr[x_, y_] := Plus[x, y]
. Then, if you define f
as f[x_, y_] := expr[x, y]
, you can do without the Block
.
In other words:
ClearAll[f1, f2, expr1, expr2, x, y];
x = 1; y = 2;
(* What you're doing now: *)
expr1 := Plus[x, y];
Block[{x, y}, f1[x_, y_] := Evaluate[expr1]]
f1[3, 4]
(* What I would do instead, if possible: *)
expr2[x_, y_] := Plus[x, y];
f2[x_, y_] := expr2[x, y];
f2[3, 4]
Side note: have you used \[FormalX]
and \[FormalY]
before? They can be handy for avoiding conflicts with global variables.