Mathematica > Core Language > Procedural Programming > Scoping Constructs > With >
Mathematica > Core Language > Package Development > Namespace Management > Scoping Constructs > With >

With

With
specifies that in expr occurrences of the symbols x, y, ... should be replaced by , , ....
  • With allows you to define local constants.
  • With replaces symbols in expr only when they do not occur as local variables inside scoping constructs.
  • You can use With as the right-hand side of a transformation rule with a condition attached.
  • With constructs can be nested in any way, with inner variables being renamed if necessary.
  • With is a scoping construct that implements read-only lexical variables.
Use With to insert values into held expressions:
The variable names can be the same:
Use a constant for a value that is needed more than once:
With allows inserting values into unevaluated expressions:
Module introduces local variables to which you can assign values:
With is faster than Module:
Block localizes values only; it does not substitute values. Module creates new symbols:
With allows substitution inside an unevaluated expression, preserving nested scopes:
Ordinary substitution does not preserve scoping:
With is a scoping construct; variables are renamed in nested scopes:
Build the function from its elements to avoid the renaming:
Find a zero of an arbitrary function using Newton's method:
Find a fixed point:
A version of With where the initializer is within the scope of the local variable:
Here the f inside the function definition is not inside its own scope:
New in 2
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF