HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
YOU LEARN HERE... |
how to define functions with parameters. You already know parameters from many Turtle commands. With the forward(s) command, you can use different numbers for s. With forward(100), Turtle moves forward 100 steps. The forward(s) command has a parameter s. Self-defined functions can also have parameters. |
EXAMPLES |
In chapter 5, you defined a function square() that draws a square with a fixed side length of 100. It is said that the side length 100 is “hard-wired” in the program.
Program: from gturtle import * def square(s): repeat 4: forward(s) left(90) makeTurtle() setPenColor("red") square(80) left(180) setPenColor("green") square(50)
Program: from gturtle import * def polygon(n, c): w = 360 / n setPenColor(c) repeat n: forward(100) left(w) makeTurtle() setPos(-50, -200) setPenWidth(3) right(90) polygon(3, "red") polygon(4, "green") polygon(5, "blue") polygon(6, "magenta") polygon(8, "cyan") polygon(10, "black") |
REMEMBER... |
If you call the function square(s) with the parameters 100, the variable s in the function square(s) receives the value 100. The parameterization of functions is important because it allows you to use the functions more flexibly. You can use the setPos(x, y) function to move the turtle to any position in the turtle window. |
TO SOLVE BY YOURSELF |
|