Definition and a call to a function
1. Function without arguments
to fname
instructions
end
2. Function with required arguments
to fname :arg1 :arg2 ...
instructions
end
3. Function with required and optional arguments
to fname :arg1 :arg2 ... [:opt1 expression1] [:opt2 expression2] ... [:rest]
instructions
end
Function (a subroutine) is a set of instructions of which execution inside the code is denoted with the
fname given to the function. A function can be called with input variables, called arguments. Arguments
can be:
- required: :arg1 :arg2 ...
, which have to be specified with the function call;
- optional: [:opt1 wyrażenie1] [:opt2 wyrażenie2] ...
, which may by specified
with the function call and in this case the whole expression has to be surrounded by parentheses; if an optional
argument is not specified with the function call then its value is set to the default, which is defined with the
expression in the function definition (and that expression can use earlier defined arguments of the
function);
- the rest: [:rest]
, which is a list of all arguments in the function call which were not
used as required nor optional arguments.
All arguments are treated as local variables when a function is executing its code.
A function can return as a result a single value by using output
instruction.
A function can exit from any place in its code by using stop
instruction.
Local variables which are created in a function code (with the let
instruction) are deleted upon exit from the function.
Global variables created inside a function (with the make
instruction
or the assignment operator :=
) remain alive also when the function is finished.
POOL uses function definitions also to define classes.
Example 1:
Simple function with no arguments and no return value.
to square
repeat 4 [fd 80 rt 90]
end
repeat 36 [fd 20 rt 10 square]
Output: graphics, no text output.
Example 2:
A function with one required and one optional argument, no return value.
to polygon :n [:l 50]
repeat :n [fd :l rt 360 / :n]
end
repeat 10 [
"x := repcount + 2
polygon :x
(polygon :x (-50))
]
Output: graphics, no text output.
Example 3:
Functions to calculate area of a rectangle and a triangle. If the optional argument is not specified, then its
default value is calculated to obtain the result for regular polygons.
to rectangle :a [:b :a]
op :a * :b
end
to triangle :a [:h (0.5 * sqrt 3) * :a]
op 0.5 * :a * :h
end
print rectangle 2
print (rectangle 2 3)
print triangle 2
print (triangle 2 3)
Output:
4
6
1.73205080756888
3
Example 4:
Recursive function.
to factorial :x
if :x > 1 [op :x * factorial :x - 1]
op 1
end
print factorial 5
Output:
120
Example 5:
Function with a variable number of arguments.
to sum_n [:r]
let "s 0
foreach "x :r ["s += :x]
op :s
end
print sum_n
print (sum_n 1)
print (sum_n 1 2)
print (sum_n 1 2 3)
Output:
0
1
3
6
See also:
let, localmake - set or create local variable
output - return result from a function
stop - exit from a function
Classes and inheritance
Turtle - object
Function as a value
$, func - get function as a value
Table of Content