eljárás name(par1,
par2, ... , parN){...instructions...}
defines a new procedure with the name you want. The procedure can have zero
or more parameters, which you may also give useful names. Here they are called
par1, par2, . . . , parN. These are the variables you can use in the instruction
between curly brackets. The code in a procedure will not be performed automatically,
you have to write a 'procedure call' every time you want to perform the instructions
in the definition (See next instruction).
Tip: create a new procedure when when you you use a sequence
of instructions more than once.
Example:
# define how to draw a rectangle
eljárás rectangle(width, height)
{
fessFehéren
ismételd(2)
{
elõre(height)
jobbra
elõre(width)
jobbra
}
neFess
}
name(arg1, arg2, . .
. , argN)
is the call to the procedure with the corresponding name and the same amount
parameters as you have arguments. The argument, here called arg1, arg2, . .
. , argN, are the particular values that will be used in the procedure definition.
Example:
# these instructions will be performed
elõre(1)
rectangle(3,2) # a call to the 'rectangle' procedure
elõre(3)
rectangle(1,4) # another call with other arguments
# this is the definition of 'rectangle'
eljárás rectangle(width, height)
{
fessFehéren
ismételd(2)
{
elõre(height)
jobbra
elõre(width)
jobbra
}
neFess
}
vissza
To stop executing a procedure before it reaches the end, use vissza from within a
eljárás definition. The program continues executing the commands after the corresponding call.
Example:
# these instructions will be performed
toWall()
jobbra
elõre
# go forward until you reach a wall
eljárás toWall()
{
ismételd
{
ha(szembenAkadály)
{
# stop "toWall" procedure,
# continue with right and forward
vissza
}
különben{
elõre
}
}
}
vissza(arg)
To stop executing a procedure before it reaches the end, use vissza from within a
eljárás definition. The program continues executing the commands after the corresponding call.
By default, a procedure return the value zero. You can change this by returning an expression.
Example:
# this instruction will be performed
elõre(double(3))
# double the given amount
eljárás double(n)
{
vissza(2 * n)
}
Recursion
Procedures can be defined recursively. That means that you use the procedure you are defining in the procedure definition itself, by calling it.
It takes a while to comprehend, but turns out to be a powerfull tool.
Example:
# these instructions will be performed
toWall()
jobbra
elõre
# go forward until you reach a wall
eljárás toWall()
{
# notice that no loops are used
ha(szembenAkadály)
{
# stop "toWall" procedure
vissza
}
különben{
# do one step
elõre
# and do a recursive call!
toWall()
}
}
|