parameter
Deutsch   English   

7. PARAMETERS

 

 

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.

The function can be used much more flexibly if you can specify the side length when calling the function, e.g. write square(50) or square(70). To do this, you must add a parameter to the function definition of square(s) the name of which you write in the parameter brackets. You can use the parameter inside the function (in the function body) like a normal variable. In the program, the turtle draws two squares with side lengths of 80 and 50.

 


Program:    

from gturtle import *

def square(s):    
     repeat 4: 
        forward(s) 
        left(90)
      
makeTurtle()
setPenColor("red")
square(80)
left(180)
setPenColor("green")
square(50)
► Copy to clipboard

 

A function can also have several parameters. In your example, you define a function polygon(n, c)c for the pen color. The function draws regular polygons with the given number of corners and color. In the main program, the function is called 6 times and draws a triangle, square, 5-corner, 6-corner, 8-corner and 10-corner in different colors.

The angle of rotation required for drawing can be calculated with 360 / n.

 

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")
► Copy to clipboard

 

 

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

 
1.

The adjacent image is created by reducing the length of the square side by a factor of 0.95 for each subsequent square, starting from 180. 100 squares are drawn. Write the program with the function square(s)

 


2.

Define a function, triangle(c), which draws a filled triangle with the given color. Use it to create the adjacent picture.


 



3.

Define a function circle(s, c) with which the Turtle draws filled circles with the given fill color and use it to create the adjacent drawing. You have already learned in the chapter Filling figures that you can draw a circle as a polygon.

repeat 120:
    forward(3)
    right(3) 
 

4.

You define a function circle(s) that draws a circle. The adjacent figure is created by drawing 30 circles, decreasing s after each circle drawn (s = s *0.9) and moving the turtle 5 steps forward.

 

5.

Define a function square(s, c) with two parameters: s for the side length and c for the fill color. Then call the function three times so that the adjacent figure is created.