A function is a list of commands for the computer to execute.
There are many built-in functions like
but you can also write your own.
-> to say that you are starting a function.
= to assign the function to a variable.
Indent the lines in the list of commands evenly.
do to run the function.
newFunction = ->
Functions can have arguments -- here, arguments aren't fights! In Pencilcode an argument is a piece of information that you give the function.
Arguments are placed in a list inside parentheses
() before the
If your function has arguments, you do not need to use
do to run it.
echo = (arg) ->
write "you said: " + arg
Functions can compute a value. The
return command finishes a function
immediately with a specific value:
cost = (n) -> if n < 10 return n * 1.99 else return n * 1.63 write '5 for', cost 5 write '50 for', cost 50