WolframLang: Local Variable

By Xah Lee. Date: . Last updated: .
Module[{var1, var2 etc}, body]
  • Eval body with temp local variables.
  • Each of the variable var can be the form var = val or you can set the value in the body by var = val
  • Return the value of body.

Module

(this is similar to “lexical scoped” construct in most programing languages, or lisp's let)

When the body is more than one expression, add semicolon between them. [see CompoundExpression and Semicolon]

x = 5;

Module[{x},
 x = 3;
 x+1] == 4

x == 5
(* x is still 5 *)
Module[{x = 3}, x+1] == 4

Later values in variable is not available:

(* later values in variable is not available *)
Module[{x=2, y=x}, y] === x

To assign variable that depends on previous variable value, put code in body:

(* to assign variable that depends on previous variable value, put code in body *)
Module[{x=2, y},
y=x;
y] == 2

Local constants

With, is similar to Module, except the variables must have a value and cannot be changed.

With[{c1 = v1, c2 = v2 etc}, body]
  • Eval body with temp local constants.
  • Return the value of body.

With

With[{x = 3}, x+1] == 4

WolframLang: Global Variable

WolframLang in Depth

Basics

Comment, Print

String

Arithmetic

List

Expression, Atom, Head

Boolean

Conditional

Variable

Loop

Data Structure

Function

Pattern Matching

Advanced

Shell Tasks

Misc