5.3 Writing Turtle Functions

Once you have designed some shapes that you think you might want to draw again, you should write them up as functions. Here for example, is the code for a function that makes squares:

Note that the user can vary the length of a side. You would use it like this:

5.3.1 Practice Exercises

  1. Write a function called star8() that makes an eight-sided star with rays of length 20 each. The user should be able to specify the thickness of the rays (think turtle_lwd()). The body of the function should begin with turtle_init() so that the star is always drawn inside a new field. The function should take a single parameter called width, the desired thickness of the rays. Typical examples of use should be as follows:

  2. Modify your star8() function to make a new function called superstar8() that takes an additional parameter called color that allows the user to specify the color of the rays. The default value of color should be "burlywood". Typical examples of use should be as follows: