Wolfram Language Fast Introduction for Programmers
Get Started »

Procedures

The Wolfram Language is an elegant fusion of many programming paradigms—including the procedural, object-oriented and functional paradigms familiar to Java programmers—allowing you the flexibility to choose and combine strategies for different projects.

Python code contains a mixture of procedural, object-oriented and functional programming. The Wolfram Language also supports a wide variety of programming paradigms, while encouraging a more functional approach.

Procedural programming is usually needed only in small doses in the Wolfram Language.
(If you have a big program full of If, Do, Return, etc., youre probably not doing things right.)

Use ; to separate different operations:

In[1]:=
X

Java requires a semicolon to terminate statements. In the Wolfram Language, the semicolon is optional and has the added functionality of suppressing output display.

Semicolons ; can be used in Python to separate different operations on one line as well. In the Wolfram Language, they have the added benefit of suppressing unnecessary output from expressions.

Use Module to localize variables:

In[2]:=
X
Out[2]=

Module introduces scoped variables similar to how methods in Java create locally scoped variables.

Module introduces scoped variables, similar to how functions and classes in Python create a separate scope from global variables.

Module does lexical scoping (localizing names).

Block does dynamic scoping (localizing values).

DynamicModule does scoping within a document.

Sow/Reap and Throw/Catch are useful ways to transfer data and control in procedural programs.

Throw and Catch in the Wolfram Language generalize Java's try and catch constructs, enabling flexible flow control for a wide range of applications. For specifically handling errors, the Wolfram Language also includes the Check function.

Throw and Catch can be used like Python's Raise and Try/Except for exception handling.

True and False are symbols in the Wolfram Language.

In[1]:=
X
Out[1]=

In the Wolfram Language, If is just another built-in function:

In[1]:=
X
Out[1]=

The Wolfram Language has many procedural functions such as While, For, If and Switch that work similarly to the Java keywords of the same names, but they are typically unnecessary in Wolfram Language code.

The Wolfram Language has many of the procedural tools a Python programmer would expect: the While and If functions operate similar to the While and If constructs in Python. However, the For iterator is not the same as the For loop in Python, instead requiring an initializer, end-condition and increment statement. It can be simulated by using a Python For loop with a range() object.

Check Your Understanding

Which of these returns False?

a = 1; b = 2; If[a > b, False, True]

Incorrect. a is not greater than b, so If returns its third argument, True.

a = 1; b = 2; If[a < b, False, True]

Correct.

a = 1; b = 2; a < b

Incorrect. a is less than b, so a < b returns True.

Which of these is the output of a = 0; Module[{a = 1}, a + 1]; a?

2

Incorrect. Nowhere is any variable a set to the value of 2.

0

Correct.

1

Incorrect. The assignment inside the Module does not affect the variable a outside of the Module.

Which of these sets x to be 2, then sets y to be 5, and then prints x + y?

x = 2 y = 5 Print[x + y]

Incorrect. Use ; to separate different operations.

x = 2; y = 5 ; Print[x + y]

Correct.

x = 2; y = 5 ; x + y;

Incorrect. The ; at the end suppresses the output so you don't see x + y.