Variables and Constants

Variables and constants are used to store values in your programs. Essentially all computer programs are a sequence of setting values, changing values, inspecting values and reacting to them. As such, variables and constants are the basis of all programming.

Variable: A value that may change over time.
Constant: A value that may not change for the duration of its lifetime.

Variables can be as simple as a counter that can be used to count from 1 to 10, or a complex structure that can store biographic data about people in a contact list.  In this latter case, the structure is itself a collection of variables, each representing a single item of data about a contact, such as first name, last name, date of birth, etc.

The difference between variables and constants is simply that variables are values that can change over time, whereas constants are expected to be fixed for the duration of their life-time. A container that stores the current time of day would be a variable, whereas one that stores the mathematical value pi should be defined as a constant. Always use constants where possible, as this reduces the risk of inadvertently changing the value somewhere in your code!

WiringSwift
The form of a constant definition in wiring is: const datatype name = value.

For example the following defines a constant called myConstant that is an integer of value 42.

const int myConstant = 42;

The form of a variable declaration is: datatype name, or: datatype name = value.

For example the following defines a variable called myVariable that is an integer of value 42.

int myVariable = 42;
The form of a constant definition in swift is: let name: datatype = value.

For example the following defines a constant called myConstant that is an integer of value 42.

let myConstant: Int = 42;

The form of a variable declaration is: var name: datatype;, or: var name: datatype = value;

For example the following defines a variable called myVariable that is an integer of value 42.

var myVariable: Int = 42;