Defining actions

Help Contents First Steps In Logo Built-in instructions Defining actions
while The Logo Turtle

Defining actions

You already know that to ask Logo to do something, you need to specify what action to do.

To teach Logo to do new things there must be a way to describe new actions.

to...end, output

The definition of an action starts with the word
	to
and ends with the word
	end
. Right after to you must say the name of the action (without " in front) and the parameters of the actions.

Parameters that are expected to be said before the name, should be declared before it.

Parameters that are expected to be said after the name, should be declared after it.

In the lines between these declarations and the word end you must include the instructions that make up the actions.

to greater :x :y
	if :x>:y [print "yes] [print "no]
end

The definition above is about an action called greater which expects two arguments after its name.

When you say this action to Logo, for example:
	greater 5 6
then it will print no.

Logo creates one local to the action variable for each argument. The names of these variables are taken from the first line of the declaration.

In this case they are x and y.

Note that you must say : before each name of a parameter.

Why? First of all, this is to remind Logo that the parameters expect values.

Secondly, this is the only way for Logo to distinguish the action's name from it's parameters' names.

The order of the parameters is decided by you and anyone that uses your action, even if this is you, must use the same order.

For example, if you define greater in another way, you should use it in exactly this other way:

to :x greater :y
	if :x>:y [print "yes] [print "no]
end

	5 greater 6

Inside an action you can do everything that you can do in Logo.

For example, you can define variables or actions.

Anyway, there is one special thing that is unique to actions.

Sometimes you may want to define an action that returns a value that can be used in further calculations.

Such a value is returned back with the instruction output.

If you wish to change the example above not to print the result of the comparison, but to return it back, then you may change the declaration in this way:

to :x greater :y
	if :x>:y [output "yes] [output "no]
end

print 5 greater 6 ; no
if (5 greater 1)="yes [print "really?] ; yes

Returning a value will make it possible to use the result of the action as it is shown in the if action above.

When Logo executes output it stops the action immediately. This effect can be used when you want to cancel an action earlier.

In such cases you can use output without saying what value to return.

while The Logo Turtle