Glich - v0.3.0 Manual
Variables & Constants
Development v0.3.0 Manual Glich Script Statements Variables & Constants

Constants, Local and Global Variables

A constant is a named value that cannot be changed after it has been initialized. A variable is a named value that can be changed after it has been initialized. A local variable is a variable that is only accessible within the block in which it is defined. A global variable is a variable that is accessible from any part of the program. In particular, functions and commands can access global variables but only local variables defined within the function or command. Constants are always global and are accessible from any part of the program.

Three separate statements are used to define constants constant, local variables let and global variables global. Once a variable has been defined, the let and global keywords may be omitted when changing its value, unless a global and local variable share the same name, in which case the global keyword must be used to change the value of the global variable.

It is an error to define a constant or variable with the same name as an existing constant or variable.

Constants and variables persist between running scripts, until the host program is terminated. They can be controlled with the mark statement.

Statement: constant

The constant statement is used to introduce a named constant and initialize it with a value. Once a constant has been defined, its value cannot be changed.

constant Statement
FormatExampleResult
constant name = expr; constant julian_cycle = 4 * 365 + 1; julian_cycle holds number value 1461

It is an error to use the same name for a constant and a variable, or to define a constant with the same name as an existing constant.

Statements: let and global

The difference between a local variable and a global variable is that functions and commands can access global variables but only local variables defined within the function or command.

The let statement is used to introduce a named local variable and initialize it or change the value of an existing local variable.

The global statement is used to introduce a named global variable and initialize it or change the value of an existing global variable.

If a local or global variable already exists then the let or global keyword at the start is optional. However, if a local and global variable share the same name then the global keyword must be used to change the value of the global variable.

let Statement
FormatExampleResult
let name = expr; let answer = 20 * 345; answer holds number value 6900
let name += expr; let answer += 50; answer holds number value 6950
let name -= expr; let answer -= 6000; answer holds number value 950
let name *= expr; let answer *= 3; answer holds number value 2850
let name /= expr; let answer /= 10; answer holds float value 285.0
name = expr; answer = 50 .. 30; answer holds range value 30..50
name @= obj_function; let obj = {: 0,,2}; obj @= mask({: 10,11,12}); obj holds object value {: 0, 11, 2}
name[index] = expr; obj[1] = 99; obj holds object value {... ..., 99, ...}
name[name] = expr; comp[imaginary] = 5; comp holds object value {complex: ..., 5}
Development v0.3.0 Manual Glich Script Statements Variables & Constants

Managed by WebPageLayout Validated by HTML Validator (based on Tidy)

9th March 2023