Function as a value

Function can be used as a value in the following cases:

Class / constructor

Function definition is used in POOL also as a class description. Turtles (objects) of a class are created with instructions newt and newturtles. Function passed as an argument of these instruction becomes the constructor of the turtle / turtles.

Example 1:

to fig :n
  to draw :a
    let "p pos
    let "h heading
    rt 90 bk :a/2
    repeat :nsides [fd :a lt 360 / :nsides]
    setpos :p
    seth :h
  "nsides := :n

"tri := (newt $fig 3)  ;triangle: constructor with an argument "3"
"pnt := (newt $fig 5)  ;pentagon: constructor with an argument "5"
print :nsides @ :tri
print :nsides @ :pnt
(draw 100) @ :tri
(draw 100) @ :pnt



See also:

Classes and inheritance
Parallel tasks of turtles

Declaration of a timer handling function

Timer in POOL allows for sending a signal or launching a code in regular time intervals. Such a code can be a list of instructions or a function passed as an argument to the timer instruction.

Example 2:

to fn :t :i :dt
  print :t

"t := (timer $fn 500 0 5)


[1 27.203125]
[2 500.1953125]
[3 501.6484375]
[4 500.5078125]
[5 500.640625]

See also:

timer - create a timer

Declaration of an event handling function

Events raised in the user interface (such as mouse actions, use of buttons, etc) can launch code. Such code can be declared with instructions like setonclick, setonchange and analogous. These instructions accept as an argument a list of instructions or a function.

Example 3:

to bhandler
  print "Click...

"b := button "Test [20 10]
setonclick :b $bhandler

Output (on each push of the "Test" button):


Dynamic call to a function

Calls to functions can be dynamic in POOL programs. It allows to choose called function during the program execution. In this case a function is passed as an argument of the call instruction.

Example 4:

to f1 :x op sqrt :x end
to f2 :x op exp :x end
to f3 :x op log :x end

repeat 3 [print (call func word "f repcount 2)]



See also:

Dynamic call to a function
$, func - function

Table of Content