পদ্ধতি 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
পদ্ধতি rectangle(width, height)
{
সাদা_রং_করো
বারবার_করো(2)
{
সামনে(height)
ডানদিকে
সামনে(width)
ডানদিকে
}
রং_বন্ধ
}
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
সামনে(1)
rectangle(3,2) # a call to the 'rectangle' procedure
সামনে(3)
rectangle(1,4) # another call with other arguments
# this is the definition of 'rectangle'
পদ্ধতি rectangle(width, height)
{
সাদা_রং_করো
বারবার_করো(2)
{
সামনে(height)
ডানদিকে
সামনে(width)
ডানদিকে
}
রং_বন্ধ
}
কাজ_শেষ
To stop executing a procedure before it reaches the end, use কাজ_শেষ from within a
পদ্ধতি definition. The program continues executing the commands after the corresponding call.
Example:
# these instructions will be performed
toWall()
ডানদিকে
সামনে
# go forward until you reach a wall
পদ্ধতি toWall()
{
বারবার_করো
{
যদি(সামনে_বাধা)
{
# stop "toWall" procedure,
# continue with right and forward
কাজ_শেষ
}
নাহলে{
সামনে
}
}
}
কাজ_শেষ(arg)
To stop executing a procedure before it reaches the end, use কাজ_শেষ from within a
পদ্ধতি 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
সামনে(double(3))
# double the given amount
পদ্ধতি double(n)
{
কাজ_শেষ(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()
ডানদিকে
সামনে
# go forward until you reach a wall
পদ্ধতি toWall()
{
# notice that no loops are used
যদি(সামনে_বাধা)
{
# stop "toWall" procedure
কাজ_শেষ
}
নাহলে{
# do one step
সামনে
# and do a recursive call!
toWall()
}
}
|