Functions for Complex Shapes

Do this on pencilcode.net

Functions can used just like built-in commands, and we can build complex functions out of other functions.

Making a twig function

After a function is defined, it can be run using do function:

pen brown
twig = ->
  fd 20
  dot green
  bk 20
do twig

Making a branch function

A function can be called from anywhere, including inside other functions. Functions work just like built-in commands.

# (add to the program above)
branch = ->
  fd 20
  lt 30
  do twig
  rt 60
  do twig
  lt 30
  bk 20
do branch

Making a tree function

Here is a function that uses a function that uses a function.

# (add to the program above)
tree = ->
  fd 50
  lt 30
  do branch
  rt 60
  do branch
  lt 30
  bk 50
do tree

You can attach functions to buttons or clicks. Try using a function to make an intricate shape inside a drawing program.