Throughout this tutorial we’ve used lots of actions to do
But, we don’t always have to depend on the actions that come with Setanta, we can make our own!
Actions are things like scríobh
and codladh
. Actions take
You ( )
and placing the arguments inside the brackets.
e.g: scríobh("Dia duit")
, ciorcal@stáitse(100, 100, 100)
, léigh()
Some actions also have
e.g: aois := ceist("Cén aois thú")
To make gníomh
The syntax for a gníomh
statement is:
Writing a gníomh
statement like this <list of argument names>
, then the code between the curly braces { }
is executed. (This code is called the
Here is a quick example: Let’s make an action called say_hello
, say_hello
will take no arguments, and write “Hey” on the console.
As you can see we have filled in say_hello
as the action name, the list of arguments is scríobh("Hey")
.
We call this new action with say_hello()
. Let’s try it out!
As you can see, calling say_hello()
caused “Hey” to be written on the console! What’s going on here?
When Setanta reads an action call like say_hello
, say_hello()
, it jumps to the code in the say_hello
action, which was scríobh("Hey")
and runs it, causing “Hey” to be written on the console.
How do we make actions that take arguments? You do this by picking names for your arguments, and then when the action is called the argument values are attached to variables with those names.
Let’s write an action to draw a mid_circle
.
Let’s start with some code to draw a circle in the middle with radius 100, and we’ll see how to change this to be an argument after.
We use fad_x@stáitse
and fad_y@stáitse
to get the midpoints and use ciorcal@stáitse
to draw the circle.
gníomh mid_circle() {
>-- Change to blue
dath@stáitse("gorm")
>-- Get midpoint
mid_x := fad_x@stáitse / 2
mid_y := fad_y@stáitse / 2
>-- Draw the circle
ciorcal@stáitse(mid_x, mid_y, 100)
}
We call this with mid_circle()
, try it out:
Now we want to get rid of that fixed radius of 100, and replace it with an argument. Let’s call the argument radius
. We place the name of the argument between the brackets ( )
in the defining line of the action:
gníomh mid_circle(radius) {
dath@stáitse("gorm")
mid_x := fad_x@stáitse / 2
mid_y := fad_y@stáitse / 2
ciorcal@stáitse(mid_x, mid_y, 100)
}
Inside the action we can now refer to the name radius
anywhere we’d like to use the argument. So let’s replace the 100 value with radius
.
gníomh mid_circle(radius) {
dath@stáitse("gorm")
mid_x := fad_x@stáitse / 2
mid_y := fad_y@stáitse / 2
ciorcal@stáitse(mid_x, mid_y, radius)
}
Now we can call mid_circle
with one argument and it’ll draw the circle for us, let’s try calling it with 200
(mid_circle(200)
).
Now we can use our mid_circle
action as many times as we want, and we don’t need to write out all that code again.
But now, what if we want to be able to change the colour of the circle too? All we have to do is add a new argument, let’s call it “colour”, and we can use that argument instead of “gorm” for the colour. To add a new argument we can put it in between the brackets after the existing argument “radius”. We use a comma to separate the two arguments. Here is our new code:
gníomh mid_circle(radius, colour) {
dath@stáitse(colour)
mid_x := fad_x@stáitse / 2
mid_y := fad_y@stáitse / 2
ciorcal@stáitse(mid_x, mid_y, radius)
}
To call this new action we have to supply two arguments, the radius and the colour. Lets change our previous code to draw a
Create an action called rand_circs
that takes 3 arguments n
, radius
and colour
, that draws n
random circles with radius radius
and colour colour
on the stage.
Now that we’ve seen how to make our own
We will use the keyword “toradh
The syntax for a toradh
statement is simple. It’s just:
To set a value as the result of the action (called “returning” the value), you simply put toradh
in front of the value.
For example, “toradh 1
”, “toradh ["a", "b", "c"]
”.
When the Setanta interpreter reads a toradh
statement, it stops executing the action
Here’s an action called return_1
that just returns 1
as it’s result, and
Look scríobh(return_1())
“1” is written on the console, this is because 1
was the result of the call to return_1()
.
It’s toradh
statement is reached, the execution of the action is stopped, and the result is returned immediately.
If we add scríobh("Setanta is fun")
after the toradh
statement in our return_1
action, we can see that it will not be called:
As you can see, "Setanta is fun"
is not written on the console. This is scríobh
call was never
Try and create an action called max_3
that takes in 3 arguments, a
, b
and c
and returns the
For example the result of max_3(10, 20, 30)
should be 30.