HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
funktionen
Deutsch   English   

5. FUNCTIONS

 

 

YOU LEARN HERE...

 

how to structure your programs with named program blocks, called functions in Python. The use of functions is of great importance, because you avoid having to write the same code several times in the program (code duplication) and you can use them to break down problems into smaller, easier-to-solve sub-problems.

 

 

EXAMPLES

 

In the previous chapters, you learned how the Turtle can draw a square. This is a typical, self-contained task and you can summarize the required code in a function.

This is particularly useful if you want to draw several squares.

 

The function definition begins with the keyword def, followed by the name, a so-called parameter bracket and a colon. The commands in the function body form a program block and must therefore be indented.

def square():
    repeat 4:
        forward(100)
        right(90)
  

In the main program, call the function by its name.

Program:    

from gturtle import *

def square():
    repeat 4: 
        forward(70) 
        left(90) 

makeTurtle()
setPenColor("red")
square()
right(120)
setPenColor("blue")
square()
right(120)
setPenColor("green")
square()
► Copy to clipboard

 

 

Once you have defined the function, you can call it as often as you like. To make the drawing appear faster, you can use hideTurtle() to make the turtle invisible right from the start.

Program:    

from gturtle import *

def square():
    repeat 4: 
        forward(60) 
        left(90) 

makeTurtle()
setPenColor("blue")
hideTurtle()
repeat 20:
    square()
    right(18)
► Copy to clipboard
 



 

 

REMEMBER...

 

You can choose the function names pretty freely, but you must adhere to a few restrictions. Allowed are names

  • without umlauts and special characters, except $ and _ (i.e. with the letters a..z, A..Z, the numbers 0..9, as well as $ and _).
  • that do not begin with a number and
  • which are not keywords.

It is common practice to begin function names with a lowercase letter and to use lowercase notation for compound names (e.g. drawFigure). English identifiers are better because they do not contain umlauts and your programs are universally readable.

 

 

TO SOLVE BY YOURSELF

  1.

Define a hexagon() function with which the Turtle draws a hexagon. Use this command to create the adjacent figure.

 


  2.

Define a command for a square that stands on its tip and use it to draw the adjacent figure.

 
  3a.

In the following task, you will see how you can solve a problem step by step using functions.

Define a function arc() with which the turtle draws an arc and turns 90 degrees to the right.

 
 
3b.


Complete the program with the function petal(), which draws two arcs. However, the Turtle should end up in the starting direction again.

 
  3c.

Extend the program with the function flower() so that an 8-petaled flower is created.

 


  3d.

Extend the function blumenblatt() so that the leaves are filled with a red color