functions

A function is a list of commands for the computer to execute.

There are many built-in functions like dot or fd, but you can also write your own.

Making a Function

Use -> to say that you are starting a function. Use = to assign the function to a variable. Indent the lines in the list of commands evenly. Use do to run the function.

newFunction = -> fd 20 rt 30 do newFunction do newFunction do newFunction

Function Arguments

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 echo "hello" echo "goodbye"

Function Return Values

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