Wolfram Computation Meets Knowledge

The Wolfram Language:
Fast Introduction for Programmers

Show additional notes for:
All sections 
Get Started

Procedures

Notes for Java programmers:

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.

Notes for Python programmers:

The Wolfram Language tightly integrates procedural, object-oriented and functional paradigms, allowing you the flexibility to select and combine strategies for different projects.

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., you're probably not doing things right.)

Use ; to separate different operations:

In[1]:=1
Print[a]; Print[b]; Print[c]
Out[1]=1

Notes for Java programmers:

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

Notes for Python programmers:

You can arrange procedures across lines in any way you want in the Wolfram Language. Unlike Python, it does not enforce particular whitespace and code-formatting rules.


Use Module to localize variables:

In[2]:=2
Module[{a = 1}, a + 8]
Out[2]=2

Notes for Java programmers:

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

Notes for Python programmers:

Functions in Python create locally scoped variables similar to Module in the Wolfram Language.


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.

Notes for Java programmers:

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.

Notes for Python programmers:

The Throw and Catch functions in the Wolfram Language work like Python's try/except constructs, though are much more general. For specifically handling errors, the Wolfram Language also includes the Check function.


True and False are symbols in the Wolfram Language:

In[1]:=1
7 > 5
Out[1]=1

Notes for Python programmers:

The Wolfram Language uses the general symbols True and False; in Python, there is a separate _bool_ property that has to be defined for every object.


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

In[1]:=1
If[7 > 5 && 2 <= 4, a, b]
Out[1]=1

Notes for Java programmers:

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.

Notes for Python programmers:

While and If in the Wolfram Language are similar to while and if in Python—though they also handle symbolic values.


Which of these returns False?


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


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

© 2017 Wolfram. All rights reserved.