Variables are one of the most important parts of programming. Without a doubt, any really serious program will use variables within it.
Variables are the main method of storing data whilst the program runs. These are, in theory, references to a memory location. In ZPE terms, these are references to a ZPEVariable object, an object which contains information regarding the value, type, owner and scope of the variable.
What variables are
ZPE has almost always had two kinds of variables: global and local or functional. These types of variable differ in scope, that is what can access it. Using variables to store information that the whole program can access is achieved using global variables. Using variables in one specific function is achieved using local variables.
Declaring variables
Variables are declared by assigning a value to a name. A name is normally a $
sign followed by a suitable name, e.g. $v
(v is chosen for the variable name here because it is short for variable, this is lazy coding and discouraged)
or $name
. A declaration is achieved using the single equals (=) sign.
A global variable is one that can be accessed anywhere in the program. In this sample, the variable $v
is declared outside of any function and, in turn, is accessible anywhere within the program.
$v = 10 function main() print($v) $v = 20 test() end function function test() print($v) end function
A local variable can be declared by declaring just within one function. In the following example, $v
will be undefined in the test function, despite being defined in the main function:
function main() $v = 10 print($v) $v = 20 test() end function function test() print($v) end function
Both types of variable scope have their advantages, but that is not for this article. There is more in the official documentation. In short, local variables are great for short-term use whereas global variables are better for sharing information that is retained throughout the program.
Declaring constants
Constants differ from variables because they never change and can only be defined once. They also differ in the way that they compile since constants are replaced with their values at compile time rather than runtime.
Some constants are also provided within ZPE, there is more on these in the official documentation.
define USERNAME = "johnsmith" function main() print(USERNAME) end function
Since constants are only declared once, they are not assigned like variables but instead they are defined. Constants can later be referenced just by using the name of the constant. These references will be replaced at compile time.