Functions
Module 1 — What is a function?
A reusable, named code block.
Definition
One Function bundles a small exercise under one name. You define it once and can then call it as often as you like - if necessary with different values each time.
Syntax:
def <name>(<parameter>):
<Anweisungen>
return <wert>
The line def …: means Functional head, everything below (indented) is the Functional fuselage. Without a call, nothing happens — the definition itself guides the code not out.
Example 1.1 — define and call a function
Loading Python…Tests
Module 2 — Parameters & Arguments
Enter values into the function.
Parameter
The names in brackets def quadrat(zahl): hot water Parameter. They are local Variables that are refilled with each call
Arguments that the caller passes.
A function can have any number of parameters — separated by commas.
Example 2.1 — a parameter
Loading Python…Tests
Example 2.2 — two parameters
Loading Python…Tests
Module 3 — return: return values
Functions deliver results — the caller catches them.
Identification
With return <wert> the function returns a result to the location where it was called. There you save the result in a
Variable and keep working with it.
The function returns its value using return The caller stores the value and, if needed, outputs it using print() out.
Definition and call
def quadrat(zahl):
return zahl * zahl
ergebnis = quadrat(6)
print(ergebnis)
The call appears below the function definition without indentation. Here you practise parameters and return values. In the next module, “Main block”, you learn how to prevent calls when a file is imported.
Example 3.1 — Store return in variable
Loading Python…Tests
Without return
Missing that return, the function returns implicitly None — so it returns “nothing.” This is usually not wanted.
Module 4 — Multiple return values
Tuple unpacking on the call side.
Identification
A function is allowed behind return Specify multiple values, separated by commas: return a, b. Python packages them into one Tuple. On the call side, they are distributed across a suitable number of variables:
summe, produkt = rechner(a, b)
For example, you can B. Area and Scope or Min and Deliver Max in one call.
Example 4.1 — catching two returns
Loading Python…Tests
TipRule of style
Define the function first. Then place input, function calls and output below the definition without indentation.
NoteTwo types of exercises
The programming tasks are deliberately mixed:
- Reading-exercises (parts 1, 2, 4, 5): Below the function definition, read the values using
input()a (introduced in the module Variables), call the function and return itprint()out. The check automatically feeds in the input and compares your output character by character — therefore output exactly what is required. - Definition-exercises (Part 3, 6): Here you define only the function — none
input(), noneprint(). The check calls your function itself with fixed values and checks them Return. So you see: you don't have to read everything in, defining the function is the core.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python tutorial — 4.7. Defining functions: docs.python.org/3/tutorial/controlflow.html#defining-functions
- Python Language Reference — Function definitions: docs.python.org/3/reference/compound_stmts.html#function-definitions
Tutorials & Manuals
- Real Python — Defining Your Own Python Functions: realpython.com/defining-your-own-python-function
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Chapter on functions.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Functions” section.