Tutorial: Types and Variables
How to store various kinds of items in memory.Primitive Types in Quorum
When programming a computer, we often say that a variable has a type. For example, suppose that I have a variable with the name "a". It could be the case that this variable is a number, like 4.2, or it could be some text that we have written for a book, like, "It was the best of times." In Quorum, there are several types, and if those types are not enough, we can define our own. In this section, we go over the various types allowed in the Quorum programming language.
integer
An integer is a number, either positive, negative, or zero, that does not have a decimal point. You can create an integer in Quorum by declaring it as follows:
integer a = 10
output a
This would create a variable, named "a", that has the value of 10. Similarly, while we can use the word "integer" before the variable named "a", we can also optionally leave that off, like so:
a = 10
output a
number
A number is similar to an integer, except that it allows decimal points. For example:
number a = 10.4
output a
Like before, the type annotation in this case is optional. We can either tell Quorum it is a number directly or we can allow Quorum to figure this out on its own. Either way, once Quorum has detected what a variable is defined as, it is fixed to that type to help prevent accidental mistakes. Like before, we can leave the type annotation off as so:
a = 10.4
output a
boolean
The type of boolean has only two possible values, true and false. We can create a boolean as follows:
boolean a = true
output a
To use a boolean, we can either create it directly, like above, using the keywords "true" or "false". Alternatively, booleans come up regularly in computers when we compare different values. For example, suppose we had two integers, a and b, and we wanted to see whether a was "bigger" than b. We could do this using the following code:
integer a = 5
integer b = 6
boolean answer = a > b
c = answer //Quorum can figure out the type, if you prefer
output c
text
The text type is designed to let you use normal written text in your computer program. You can type any phrase, from a phrase to a page in a book. You can create a variable of type text as follows:
text a = "Hello, World!"
output a
Type conversion
Sometimes you need to convert one type to another. This is called converting types, or more commonly, type casting. Suppose, for example, that you had text like so:
text someText = "4.2"
output someText
This text has the value of 4.2, which looks like a number would, but how would you convert it? In Quorum, you do this using the cast action:
text someText = "4.2"
number someNumber = cast(number, someText)
output someNumber
The word cast here indicates to the computer that you are requesting that the value of "someText" be converted into an actual number. Keep in mind that we can pass invalid values here, like "stuff" that is not a valid number. In these cases, our program alert us to the fact that there is an error on the system. We can learn more about errors on theIn these cases, our program alert us to the fact that there is an error on the system. We can learn more about errors on the errors page.You can learn more about casting on the type casting page.
Variable names
While you can create variables of many types, the names have a few rules. First, variables can start with a letter or underscore, but generally letters are good practice. Past the first letter, you can use letters, numbers, or underscores, with the exception of the final character. The final one cannot be an underscore, which is because the programming language uses that character behind the scenes in special ways. For example, any of the following names would be allowed:
integer sally = 5
integer billy_likes_sally = 10
integer billy_likes_sally2 = 15
output sally
output billy_likes_sally
output billy_likes_sally2
However, none of the following names are allowed:
integer 5sally = 5 //a number cannot be the first character
integer sally_ = 10 //an underscore cannot be the final character
integer sall&&%%^$*#(y //not all characters are allowed to be used in names
Next Tutorial
In the next tutorial, we will discuss operators, which describes how to apply mathematical operators to types.