HomeTurtlegraficsGPanelRobotics WebTigerPython |
Python - Online |
Deutsch English |
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 |
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:
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:
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:
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:
|
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) |
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. |