Repository navigation
Variables
In this chapter you will learn how to declare, set and use variables! Tic's datatypes will also be explained.
After this chapter you will be able to make simplistic but working programs using Tic!
Now that we've got the compiler up and working we can get started with the real stuff. Like in many languages this starts with variables.
Something worth noting before we get started is that Tic is a statically typed language. This means that if you declare a variable as a number, it will stay a number for the rest of its lifetime. Doing stuff like assigning a string to a number variable is an error.
So how do we declare these variables? That's an easy quesion: using the Declare keyword.
Let's look at some examples:
Declare {number} a
Declare {string} b
Declare {bool} c
You might've already guessed what happens here: The variables a, b and c are declared as a number, string and bool. The parts with the curly braces are what's called the hints. These hints define what type of variable you're declaring and as you'll see later what kind of parameters a function has.
When declaring a variable you can also assign an initial value like so:
Declare {string} mystring = "Hello World!"
Now, mystring contains "Hello World!"! Easy stuff right? If you want, you can even let the compiler detect the datatype you're using like this:
Declare mystring = "Hello World!"
This is the same as the example before.
Another feature of hints you'll probably wanna use is a constant. A variable declared as a constant cannot be modified during the rest of its lifetime, as the name suggest. These are defined by adding "constant" to the hint like this:
Declare {constant bool} alwaystrue = true
No rocket science so far, eh? Let's do some basic arithmetic to show off these variables.
Declare {number} a = 1
Declare {number} b = 2
PrintLine a + b # outputs: "3"
PrintLine a * b # outputs: "2"
PrintLine a / b # outputs: "0.5"
Finally some less simplistic code. And as a bonus you've just learned another important feature: comments.
Comments can (like in most programming languages) appear in two forms: One-line comments and block comments:
# This is a one-line comment. Everything after the '#' on the same line will be ignored.
#* This is a block-comment.
Everything between the beginning
and the end is ignored as well!
*#
So far, we've only been able to use the variable's initial values. That kind of defeats the purpose of the constant hint, doesn't it? So let's change that.
The way we set a new value to an existing variable is again really straight-forward: With the Set keyword. Here's an example:
Declare {string} mystring = "Hello "
Print mystring
Set mystring = "World!"
PrintLine mystring
# output:
# Hello World!
It might be good to note, however, that setting a value of a different type than the declared type is an error. This won't work:
Declare {bool} mybool = true
Set mybool = "wrong"
So now we know how to declare, set and use variables of any type supported by Tic. The next step: If statements.
[**3. If-Statements**](https://github.com/SjVer/Tic/wiki/If-Statements)
THIS COURSE IS STILL IN DEVELOPMENT. THE NEXT CHAPTERS ARE BEING WORKED ON.
-
Control Flow
-
Functions
-
Advanced Keywords