Think about this program, what does it write?
Try it out now, were you right?
Now look at this program: there is a very small difference between it and the previous program. Think again about what it prints.
Try it out again:
Were you right?
The first program printed “3” and “3”, but the second program printed “3” and “2”. What is the difference between them?
Notice the third line in both programs:
x = 3
.x := 3
.As you know, :=
creates a new variable and =
puts a new value in some variable. This is the difference between the two programs.
Lets look again at the first program:
The
x := 2
is created on the first line.x > 1
is true so the code in the má
x = 3
changes the value of x
to 3
.x
, which equals 3
, on the console.x
again.Now let’s look at the second program:
x := 2
is created on the first line.x > 1
is true so the code in the má
block is run.x := 3
is created.scríobh(x)
is run, but what does x
mean here? We have two variables called x
. When we ran the program earlier it printed “3”, so x
must mean the second variable, the one that is defined in the má
block.scríobh(x)
is the last line too, what does x
mean here? If you run the program this line will print “2”: This x
must mean the x
on the first line.What is happening here?
When you create a variable, what are the places in the program you can use that variable? We call these places the
For example, look at this code:
We can use the variable country
on line 3 because line 3 comes after line 2. The following program is not correct because we cannot use the variable before we use it.
Things get more complex when we use má
, le idir
, nuair-a
and actions.
Look at this code:
Is that code correct? It’s not; The scope of x
is limited; When you create a variable in a block of code (code between {
and }
) you cannot use that variable from the outside. However, you can use it in any blocks that are
These
ainm := "Niamh"
gníomh dia_duit() {
scríobh("Dia duit", ainm)
}
dia_duit()
ainm = "Oisín"
dia_duit()
má 1 == 1 {
ainm := "Fionn"
dia_duit()
}
Try it out!
Were you correct? The line ainm = "Oisín"
worked, but the line ainm := "Fionn"
didn’t do anything. This happens because the ainm
variable in the body of dia_duit
is in the scope of the first variable ainm
, and the line ainm := "Fionn"
creates a different variable with a different scope: It has no effect.
In this picture each ainm
variable isn’t used anywhere.
What happens when we create 2 variables with the same name? We saw this already in the program at the top of the page:
The 4th line (scríobh(x)
) looks like it’s in the scope of both x
variables; The one on the first line and the one on the third line. In this case it uses
It’s as if the second variable
However: in general it is bad practice to create a variable that hides another variable. You should use a unique name for each variable: Things get
NB: You cannot create a new variable with a name that is already in use in the same block. For example, the following code does not work:
An action can call itself, we call this process
When write_number(10)
is called, it writes “10” on the console, it then checks if 10
is greater than 1
, which of course it is so it calls write_number(9)
. This writes “9” and then calls write_number(8)
, then that calls write_number(7)
, write_number(6)
until it reaches write_number(1)
. At that point x > 1
is not true anymore so it stops.
If a variable is created in the action, a unique version of this variable is created every time the action is called.
When we call f(2)
it creates a new variable y
that is equal to 2
. Then it checks if x > 1
which it is. Then it calls f(1)
. A new variable is created call y
that is equal to 1
, this variable is different from the variable y
we created earlier. 1
is not greater than 1
so we do not call f(0)
. Instead we write y
on the console, which writes “1”. Now we go back to the previous call f(2)
and we continue on. scríobh(y)
is the next line, but y
is not equal to 1
here, it’s equal to 2
because this refers to the first y
we created, not the second. This then writes “2” on the console.
Setanta has one special scope, x
, mo_ghníomh
and MoChreatlach
in the following program have global scope. However, the variable áitiúil
does not.
creatlach MoChreatlach {
gníomh ainm() {
scríobh("Setanta")
}
}
gníomh mo_ghníomh() {
áitiúil := "Dia duit"
toradh áitiúil
}
x := 3
Global variables are special, because we can use them before we create them when we use them in an action. Lets look at some examples:
This code works: Try it out!
Code like this only works in the global scope, for example, code like this does not work. If you run it you will get an error:
We can use this to create two actions like this: This pair of actions is a bit strange, they do work but it is worth explaining how.
gníomh number_is_even(x) {
má x == 0 {
toradh fíor
}
toradh number_is_odd(x - 1)
}
gníomh number_is_odd(x) {
má x == 0 {
toradh bréag
}
toradh number_is_even(x - 1)
}
The action number_is_odd
calls number_is_even
and the action number_is_even
calls number_is_odd
. This works because number_is_even
can number_is_odd
All there is to do now is to go to the editor and try out the language. There are some examples and more documentation available on docs.try-setanta.ie.
If you need any information, or if you have any problems, you can send an email to help@try-setanta.ie.