A function is a part of a program that does not run until you want it to.
Here is how you make a function that is run whenever you click on a button. What does it do?
button "Forward", -> fd 10
1. The indented code after the arrow
-> is a function
that is not run right away.
2. The command
button "Forward", function
tells the computer to create a button, and
then to run the function whenever the button
is clicked.
A program can set up any number of button functions:
button "Forward", -> fd 10 button "Red", -> pen red
Try creating a set of buttons to control the turtle:
pen null
to turn off the pen.
You can create a function with any number of lines of code. In CoffeeScript, it is important to indent a function neatly when you do this.
button "Star", ->
for [1..5]
fd 100
rt 144
button "Loops", ->
rt 360, 50
lt 360, 50