Web Turtle - Subroutine Example

Breaking A Picture Into Objects:

Imagine you have a picture with some triangles and squares in it:
COLOR RED
REPEAT 4
  DRAW 50
  RIGHT 90
NEXT

MOVE 70

COLOR GREEN
REPEAT 3
  DRAW 50
  RIGHT 240
NEXT

RIGHT 90
MOVE 70

COLOR BLACK
REPEAT 4
  DRAW 50
  RIGHT 90
NEXT

END
(Draw it)

It's simpler to break this program up into objects:

COLOR RED
GO SQUARE

MOVE 70

COLOR GREEN
GO TRIANGLE

RIGHT 90
MOVE 70

COLOR BLACK
GO SQUARE

END

# SQUARE
  REPEAT 4
    DRAW 50
    RIGHT 90
  NEXT
RETURN

# TRIANGLE
  REPEAT 3
    DRAW 50
    RIGHT 240
  NEXT
RETURN

(Draw it)

Instant Changes:

Now, if you want your squares to be circles instead, you only need to change the code in one place, not two:


# SQUARE
  REPEAT 36
    DRAW 5
    RIGHT 10
  NEXT
RETURN

(Draw it)


"Web Turtle," created by Bill Kendrick, 1997-2017.