Variables
Variables are named storage items, which can contain data based on their
data type. They are declared and defined using the
var
keyword, which is followed by the name of the variable, its type and optionally a
value.
They may also be constant and immutable, which means that they can not be changed after they have
been defined. This is done by using the const
keyword instead of var
.
Syntax
Declaration
Definition
- Dynamic:
- Constant (One-time definitions which can never be changed):
Difference - Declarations & Definitions
Declarations
A declaration is the process of defining a variable, but without setting a value. This is done by specifying the name and type of the variable, but leaving it as is without any assignment expression.
Definitions
A definition is the process of defining a variable, but this time with a value. This is done by
specifying the name, type and value of the variable, which is done by using the assignment operator
=
.
Note though once a variable has been declared or defined, it may not be re-declared or re-defined in the same scope. Only reassignments using a assignment expression are allowed.
How to declare variables in Kipper
To declare a variable you simply specify its name and its type, but don't set a value yet:
In this case, we have now also defined the scope and visibility of the variable, as the direct parent (either a code block or the root of the file) is now able to see the variable. These scopes are explained more in-depth here: Scopes and Visibility of Variables.
Though, due to the fact we have not set a value yet, attempting to read from a variable will result in a compiler error, as you can not read from a variable with no value.
Important
You may re-declare a variable as often as you want, as long as the types match and the scope is identical. If they are not, it will result in a compiler error!
✓ VALID CODE
X INVALID CODE
How to define variables in Kipper
Defining a variable is fairly straightforward in Kipper, as like in declarations you specify the
name, type and now as well the value. Here we use the assign character =
, which sets
the variable to the VALUE
:
Though, in case you want to make clear that you have already declared the variable and don't want to specify the type again, the following is also valid code:
Important
When you already have defined or declared a variable, you may not change its type anymore, but only overwrite its value with the same type!
✓ VALID CODE
X INVALID CODE
Scopes and Visibility of Variables
Before we can get to using our own variables, the concept of so-called scopes has to be understood. A scope of a variable defines simply where your code can access a variable, and not interfere with other variables.
In this code-block, you may only reference/use variables that were defines in this scope, or a scope that is above it (parent scopes).
The scope that you can always access is the highest scope and is called the "global scope". It's where you define variables for your entire file. An example of a global would be simply this:
If you now try to access a variable from another scope, it will result in a compiler error, as the variable is not visible in your current scope:
Constants
Besides, the already explained variables that may change throughout the code, there are also the so-called constants, which are defined once with a value and may never be re-defined or overwritten.
How to define a constant variable?
To define a constant variable, you simply use the const
keyword in place of the
var
keyword, defining the variable as a constant that is protected from changes (You
may not declare a constant variable though, and you will always have to specify its value right when
creating the variable!):
You may read from the constant value like with a standard variable:
You may not overwrite the value of a constant or declare it without a value.
✓ VALID CODE
X INVALID CODE