Expressions
An expression is a piece of code that produces a value. Expressions can use integer literals, variable names, function calls, and operators:
var width = 10
var height = 20
var area = width * height
In the last line, width * height is an expression. Matchbox evaluates it and uses the resulting value to initialize area.
Integer Literals
An integer written directly in source code is an expression:
var answer = 42
var mask = 0b1111
var color = 0xFF00
See Integer Literals for the supported notations and digit separators.
Variable Names
A variable name can be used as an expression after the variable has received a value:
var subtotal = 40
var total = subtotal + 2
Here, subtotal produces the value stored in that variable.
Operators
Operators combine or transform values:
var sum = 10 + 20
var area = 6 * 7
var remainder = 17 % 5
var negative = -sum
See Operators for the complete list of arithmetic, bitwise, assignment, and unary operators, including their precedence and edge cases.
Function Calls
A call to a function that returns a value can be used as an expression:
var larger = max(10, 20)
var distance = abs(-12)
The arguments passed to a function are expressions too. Expressions can therefore be nested:
var limited = clamp(abs(-120), 0, 100)
Parentheses
Use parentheses to group part of an expression and make the intended evaluation order clear:
var result = (10 + 20) * 3
Without the parentheses, multiplication and addition may be evaluated in a different order.
Matchbox evaluates binary operands from left to right. Binary operators at the same precedence level are left-associative in 0.2.
Expressions and Statements
An expression produces a value, while a statement performs an action. For example, the right side of an assignment is an expression, but the complete assignment is a statement:
var total = 0
total = 10 + 20
Function calls can also be used as statements when their result does not need to be stored:
print(total)
Next Step
Continue to Functions.