HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
Deutsch   English   

4. Functions

 

 

YOU LEARN HERE...

 

Functions are named program blocks with which you can structure programs better. 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 can thus break down problems into smaller, simpler sub-problems.

 

 

EXAMPLES

 


Example 1
: Making a traffic light flash in several colors

A “flashing process” with the color c consists of the following actions: select the color, draw the filled circle with this color, wait 500 milliseconds, select the white color, draw over the filled circle with white color (and thus delete it) and wait 500 milliseconds. You can combine the flashing process in a function (a named block) and call this function with a different color each time.

The function definition begins with the keyword def, followed by the name, a parameter bracket and a colon. The parenthesis can contain parameters (here c) that are used in the function. If no parameters are required, the bracket is empty. The commands in the function body form a program block and must therefore be indented.

The function definitions are located at the top of the program, usually immediately after the import lines. In the main program, the function is called three times with the parameter values “red”, “yellow” and “green” .

Program:     

# Gp4a.py
from gpanel import *

def flash(c):
    setColor(c)
    fillCircle(3)
    delay(500)
    setColor("white")
    fillCircle(3.2)
    delay(500)

makeGPanel(-10, 10, -10, 10)
repeat 10:
    flash("red")
    flash("yellow")
    flash("green")
► Copy to clipboard
 




Example 2
: Extending the command set

The GPanel module already contains many predefined functions. You can extend this command set as required with self-defined functions. In this example, you define a star(x, y) function that draws a star using two filled equilateral triangles with the same center x, y. You call this function several times in the main program.

Program:      

# Gp4b.py
from gpanel import *

def star(x, y):
    fillTriangle(x - 0.86, y - 0.5, 
                 x + 0.86, y - 0.5, x, y + 1)
    fillTriangle(x - 0.86, y + 0.5, 
                 x + 0.86, y + 0.5, x, y - 1)
    
makeGPanel(0, 10, 0, 10)
setColor("red")
star(2, 2)
star(5, 7) 
star(7, 4)  
► Copy to clipboard
 

 

Example 3: Drawing random stars on a blue background (starry sky)

The star() function is copied unchanged, but the positions are chosen randomly. With bgColor("darkblue") you can set the background color of the graphics window to dark blue.

Program:      

# Gp4c.py
from gpanel import *
from random import randint

def star(x, y):
    fillTriangle(x - 0.86, y - 0.5, 
                 x + 0.86, y - 0.5, x, y + 1)
    fillTriangle(x - 0.86, y + 0.5, 
                 x + 0.86, y + 0.5, x, y - 1)
    
makeGPanel(0, 25, 0, 25)
bgColor("darkblue")
setColor("yellow")
repeat 30:
    x = randint(3, 22)
    y = randint(3, 22) 
    star(x, y)     
► Copy to clipboard
 

 

Example 4: Solving problems step by step

This simple example shows how to solve problems step by step. Breaking down a complex problem into simple sub-problems is an important programming concept (also known as structured programming). You can practise it using simple examples. Functions play a central role here, which is why the concept is also called functional or procedural programming. (In Python, functions and procedures are the same thing).

To create a flower garden, which is a relatively complex task, you define the flower() function, which draws a single flower. This consists of a straight stem and a blossom, which you define with the bloom() function. Finally, you define the function garden(n) , which draws n flowers at a random x-position.

Program:      

# Gp4d.py
from gpanel import *
from random import randint

def bloom():
    setColor("magenta")
    fillCircle(2)
    setColor("yellow")
    fillCircle(1)

def flower(x):
    pos(x, 0)
    lineWidth(3)
    setColor("green")
    line(x, 0, x, 4)
    pos(x, 6)
    bloom()
    
def garden(n):
    repeat n:
        x = randint(0, 30)
        flower(x)    

makeGPanel(-3, 32, -10, 25)
garden(10)
► Copy to clipboard
 

 

 

REMEMBER YOU...

 

You can use functions to structure programs better and extend the command set. The function name is written with a small initial letter and can be freely chosen. Keywords and words with umlauts are not permitted. The function is never executed without a function call.

 

 

TO SOLVE BY YOURSELF

 

1)


50 red, 50 blue, 50 green, 50 yellow and 50 violet colored circles should appear at random positions. Define a function colorCircle(c) with the following actions:
- sets the color to Color c
- sets the position to randomly selected coordinates in the range 0 to 100
- draw a small filled circle

In the main program, use a repeat 50: loop and call the colorCircle() function in the loop with the parameters “red”, “yellow” etc.

 

 

2)

Define a function bird(x, y) that draws a bird-like line at the position x, y. To do this, use the two arc functions arc(2, 30, 120) and arc(2, 150, -120) in the GPanel with the coordinates (0, 20, 0, 20).
 

 

3)

Define a function snowman(x) that draws a white snowman at the position (x, 0).

In the main program, select a blue background and call the function several times at different positions.