This question already has an answer here:

I've been working heavily with Block[] for months in my research so I've read the documentation for the seemingly similar commands With[], Block[], and Module[].

Personally, I've had by far the greatest success with Block, despite With having the connotation of what I think I am trying to accomplish. Sometimes With will solve a problem where Block works as well, but often not. I haven't tried Module as much, but the documentation seems so similar.

I am sure there are very critical subtleties which make these three commands have their respective places in the world and I fear choosing the wrong one may cause problems.

My incentive for using these types of commands typically is, I work with expressions of several variables (large matrices with 7 variable dependencies), and often I wish to (temporarily) examine a lower-dimensional subspace of my expression; i.e. I may wish to "substitute" numerical values for 5 or 6 of the variables to produce a simpler expression which depends only on 2 or 1 variables. Of course, it is important for me to retain the true expression (with 7 variables) so I don't simply want to set values for the variables (I could do this and then use Clear, but this gets messy very quickly). Also, I have found that using rule substitutions like


often cause mathematica to treat x as 1.45 elsewhere. I need local variable assignments, so it seems Block, With, or Module is the way to go. But how should I chose?

(* ==========================  EDIT  ========================== *)  

When Module, for example, is wrapped inside another module, the text becomes red. Does this indicate a problem or is it normal/functioning properly?

Module[{x = 4}, (x^2 + y^2) Module[{x = 1}, (x + y)]]  
Block[{x = 4}, (x^2 + y^2) Block[{x = 1}, (x + y)]]
With[{x = 4}, (x^2 + y^2) With[{x = 1}, (x + y)]]  

all of the above seem to return the desired output of


but mathematica gives slightly different variable colorings. Maybe the red isn't bad?


marked as duplicate by rm -rf Feb 27 '14 at 21:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ There is a short tutorial on the difference between Block and Module, which I guess I understand a little bit, but I still don't know how With works differently or totally understand this article. $\endgroup$ – Steve Feb 27 '14 at 21:26
  • $\begingroup$ Steve, please look at the linked duplicate above. Leonid's answer goes into a lot of detail on the differences. $\endgroup$ – rm -rf Feb 27 '14 at 21:28
  • $\begingroup$ Awesome, yeah, there's a lot there. Should I delete this question? $\endgroup$ – Steve Feb 27 '14 at 21:31
  • $\begingroup$ Generally we leave duplicates around because it helps with Google keywords for searches, but if you'd rather have it deleted, feel free to do so :) $\endgroup$ – rm -rf Feb 27 '14 at 21:33
  • $\begingroup$ Ok thanks. Actually I do have a slightly new question that I'll edit into the original post. $\endgroup$ – Steve Feb 27 '14 at 21:42