Topic 9 - Turtle Crunches Numbers

You can make a function more general by sending a number to it when you use it to pass an instruction to the Turtle. This number might represent something within it such as the length of a side or the angle of a turn.

For example:

TO SQUAREV :LENGTH
REPEAT 4 [FD :LENGTH LT 90]
END

The technical name for a value such as LENGTH is 'parameter'. Notice how in Logo the name of the parameter is preceded by a colon (:). Whenever you refer to a value passed to a function by a parameter you must place a colon immediately in front.

You can now draw a square of any size by writing:

SQUAREV 20

or:

SQUAREV 60

and so on.

 

 

 

 

We can re-write SQUAREV and pass TWO values, one for the length of the side and the other for the number of sides.

TO SQUAREVN :LENGTH :SIDES
REPEAT :SIDES [FD :LENGTH LT 90]
END

This doesn't get us very far because we know a square has 4 sides and an internal angle of 90 degrees. Wouldn't it be nice if we could draw ANY regular polygon with just one command?

We don't need to do much to our SQUAREVN function to achieve this:

TO POLYGON :SIDES :LENGTH
REPEAT :SIDES [FD :LENGTH LT 360/:SIDES]
END

Notice how we calculate the internal angle of the polygon as 360/:SIDES. The internal angle of a square is 360/4=90 degrees, the internal angle of a hexagon is 360/6= 60 degrees, and so on. We generalise this to 360 divided by the number of sides in the polygon.

Now we can draw a 6-sided polygon:

POLYGON 6 30

Or a ten-sided polygon:

POLYGON 10 25

And so on.

Much of mathematical thinking consists of moving from the particular to the general - if you can draw n shapes with n routines, can you draw the same shapes with 1 routine?

 

 

 

 

Exercises

Rewrite your flower function from the previous lesson with parameters so that flowers of any size can be drawn. Create a garden by placing a number of flowers together. Later we will add some trees.

Return to Logo Home Page