WolframLang: Local Variable

By Xah Lee. Date: . Last updated: .

To create local variables, use Module[{var1, var2 etc}, expr] . Each of the var1 can have the form var1=val1

Module[{var1, var2 etc}, body]
  • Eval body with temp local variables with local values.
  • 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.

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

Module

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

x = 5;

Module[{x},
 x = 3;
 x^2]
(* 9 *)

x
(* x is still 5 *)
Module[{x = 3}, x^2]
(* 9 *)

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

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

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

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