![]()
INTRODUCTION |
When using the command forward() you tell the turtle how far you want it to go, putting the value in the parentheses. Therefore, the value in the parentheses indicates how far the turtle will go forward. It specifies details of the command and is called a parameter: In this case, it is a number that can vary every time that you use forward(). In the previous chapter you defined your own command square(), but in contrast to forward(), the side length of the square is always 100 pixels. In many different cases it would be quite convenient to be able to adjust the side length of the square. How would you do that? |
COMMANDS WITH PARAMETERS |
from gturtle import * def square(sidelength): repeat 4: forward(sidelength) left(90) makeTurtle() setPenColor("red") square(80) left(180) setPenColor("green") square(50)
|
MEMO |
|
Parameters are placeholders for values, which can be different each time. When defining a command, you put parameters inside a pair of parentheses after the command name.
def commandname(parameter): Instructions that use parameter commandname(123)
|
MULTIPLE PARAMETERS |
from gturtle import * def square(sidelength, color): setPenColor(color) repeat 4: forward(sidelength) left(90) makeTurtle() square(100, "red") left(120) square(80, "green") left(120) square(60, "violet")
|
MEMO |
|
Commands can have multiple parameters. The parameters are written in the parentheses and separated by commas.
def commandname(parameter1, parameter2, ...): Instructions that use parameter1 and parameter2 |
EXERCISES |
|