Variables
Variables are data items, which can contain data based on their data type. The value of a variable is dynamic and can be changed as often as you want throughout the code. Variables are essential for a program to store data, read data and use data inside our programs.
What are Declarations and Definitions?
There are two ways to create a variable in Kipper, like in many other languages as well. This is the simple concept of so-called declarations and definitions.
- Declaring a variable = Reserving the name of the variable and setting its type.
- Defining a variable = Defining and setting the value of a variable. Can be combined with the declaration in a single line.
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, which will be explained later in 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 scope).
The scope that you can always access is the highest scope and is called "global scope". It's where you define variables for your entire file/program. An example of a global would be simply this:
Though, if you try to access a variable in another scope, you will get a compiler error, as you may
not access variables from other scopes. They are invisible to you, so you will get an
Undefined Reference
error, saying the value is not defined in your or any other parent
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