# Problem with With [duplicate]

I am puzzled byWith. it does not seem to work. Please see example below (I have also used Evaluate and N on it, to no avail).

force[x_] := a x - b x^3

With[{a = 1, b = 1}, force[1]]

a - b


Being naive, I would expect to see 0 as a result; a and b should have been transformed into 1 by With.

• Use Block instead.
– Kuba
Commented Feb 10, 2014 at 11:49
• You might be interested in Leonid Shifrin's answer to this question Commented Feb 10, 2014 at 20:00

With replaces occurrences of declared constants with their values, but it won't change the definition of force[] outside its scope. To do that you'd need to include the function definition like so :-

With[{a = 1, b = 1},
force[x_] := a x - b x^3;
force[1]]

0


Another option is to use Block, which does dynamic scoping

Block[{a = 1, b = 1}, force[1]]

0


• @Alberto - In your original form the definition of force[] can be found in DownValues@force. With simply doesn't affect that definition, being defined outside its scope. Commented Feb 10, 2014 at 13:14
• @Alberto, what you are describing is dynamic scoping, and Block is what does that. With is for a different purpose