One of the most crucial parts of any program is being able to take in data and store it in the computers main memory (RAM).
Computer memory is organised into bit groups, where each transistor is represented by one bit. Those bits are then organised into groups of 8 to represent one byte. Computer programs use this same memory structure to store information whilst a program is running, including the program code itself. A program stores information in what is called a variable. A variable is an abstract representation of memory. In many programming langauges the variable is given a type by the programmer. This type tells the compiler how much memory to allocate within the main memory to that variable. For instance, an integer (a whole number) is given 32-bits of memory to store a whole number (this allows it to store a number from -2,147,483,648 to or 2,147,483,648).
Data types
There are several common data types across programming languages, but often
there is at least some variance. Programming languages often define
primitive types that other data types use to
define themselves. For example, some programming languages define a
char
and a string
data type. The string
data type is several
char
values put together to make a word or sentence. The
string
type is not a primitive type since it is made
of many char
values, however, the char
data type is indeed a primitive type.
By using well-defined primitive types, the compiler simplifies compilation. It also ensures that these values are memory safe and suitable for use in real world programs.
The following table shows some of the data types you might find in a programming language:
Type | Purpose | Example |
---|---|---|
Integer | Holds a 32 bit whole number up to 31 bits in length ranging from -2,147,483,648 (-2^31) to 2,147,483,648 (2^31). Since it is a 32 bit number, 1 bit is called the sign bit. This bit stores whether or not the number is positive or negative. | 23 |
Real or Double | Holds a 64 bit double precision real value. This is a value with a decimal place. To store the value, the first value is again a signed bit to store whether the number is positive or negative. The next 11 values are the exponent and are used to represent the exponent of the number. Finally, the last 52 that are left over are the mantissa or the fraction of the number. | 65.32463 |
Boolean | Boolean values are ones which are either true or false. Boolean is one of the simplest, if not the simplest abstractions available within a computer program. Noting that a transistor has two states, on or off and that a bit is either a 0 or a 1, a boolean value is one that is true or false. | True |
Character | A single character or symbol from the keyboard. E.g. '@' or 'a'. | 'A' |
Variables
As previously mentioned, a variable is an abstraction over computer memory. In reality, a variable is nothing more than a pointer to some memory location.
The following is a simple piece of mathematics in YASS can be done with the print statement:
print(5 * 5)
However, what if the user was to input a value that is multiplied by 5? The first way
to do this is to run it as a single expression (the YASS get_input
function
is used to get an input from a user):
print(get_input() * 5)
But what if the same value is required again later. For example, what if this program was to multiply the value by 5 and then multiply the same number by 10? In this case, a variable would be required.
A variable is assigned a value and it is referenced throughout a program. In this case, the user's input can be assigned to a variable:
$user_input = get_input()
Once the information has been saved, in this case in a variable called
$user_input
, it can be later referenced which allows
the program to recall the value from the memory. The following example shows
variable assignment and recalling the value from the variable to complete the
above requirement of multiplying the same number by 5 and then by 10.
$user_input = get_input() print($user_input * 5) print($user_input * 10)
Constants
Constants are similar to variables but not the same. Constants are assigned a value in the same sense that variables are, but they differ from variables in that they cannot be changed. Furthermore, constants are often assigned during compilation and are immediately replaced within the code.
In YASS, constants are declared with the define
keyword in front of them:
define AGE = 10 print(AGE)
First class citizens
In programming, a first-class citizen is a entity which supports all operations such as being passed as an argument, modified, assigned to a variable or returned from a function.
For example, a string value can be assigned to a variable. However, in many languages, function themselves can also be assigned to variables. This makes both strings and functions first-class citizens. In almost every language, all the primitive types within that language will be first-class citizens.