werkbank
Modul 00

Vom Wert zur Funktion

Modul00
Tier1 · Drill
Drill-Zone✋ VON HAND
Repo-Zone✋ VON HAND
Python3.14 · Pyodide 314.0.3
Dauer20 + 0 min
ADRL1
Spine—

02

Worauf das aufbaut

Voraussetzung: keine

Das ist der Anfang. Dein Programm kann noch nichts.

03

Die Frage

Warum dieses Modul existiert

Weil das hier vorher gefehlt hat.

Die erste Version dieses Kurses verlangte in Modul 00 eine Funktion — und hatte nie gezeigt, was eine Funktion ist. Das war ein Fehler im Kursaufbau, kein Mangel bei dir. Dieses Modul schliesst genau diese Lücke, und zwar bei dem, was du schon kannst: print(...) und x = 5.

Danach ist der Rest des Kurses erreichbar. Vorher nicht.

Was du schon hast

netto = 84.03
print(netto * 1.19)

Das kannst du lesen und schreiben. netto ist ein Name für einen Wert, und du kannst mit dem Namen rechnen, als wäre er die Zahl.

Das Problem sitzt nicht in dieser Zeile. Es sitzt in der zweiten:

netto = 84.03
print(netto * 1.19)

netto = 12.50
print(netto * 1.19)

netto = 7.99
print(netto * 1.19)

Der Rechenweg ist dreimal derselbe. Nur der Wert wechselt. Und * 1.19 steht dreimal da — wenn die Mehrwertsteuer sich ändert, musst du drei Stellen finden und alle drei richtig ändern. Bei 400 Zeilen findest du 399.

Was eine Funktion ist

Eine Funktion ist ein Rezept mit einer Lücke.

Du schreibst den Rechenweg einmal auf und lässt genau die Stelle frei, an der sich der Wert ändert. Diese Lücke hat einen Namen — sie heisst Parameter. Und wenn du das Rezept benutzt, füllst du die Lücke.

Das Beispiel darunter zeigt es Zeile für Zeile. Lies es ganz durch, bevor du selbst tippst.

Why this module exists

Because it was missing before.

The first version of this course asked for a function in module 00 — and had never shown what a function is. That was a flaw in the course, not a shortfall in you. This module closes exactly that gap, starting from what you already have: print(...) and x = 5.

After this, the rest of the course is reachable. Before it, it wasn't.

What you already have

netto = 84.03
print(netto * 1.19)

You can read and write this. netto is a name for a value, and you can calculate with the name as if it were the number.

The problem isn't in that line. It's in the second one:

netto = 84.03
print(netto * 1.19)

netto = 12.50
print(netto * 1.19)

netto = 7.99
print(netto * 1.19)

The calculation is the same all three times. Only the value changes. And * 1.19 appears three times — if the VAT rate changes, you have to find three places and get all three right. With 400 lines you'll find 399.

What a function is

A function is a recipe with a gap.

You write the calculation down once and leave exactly the spot blank where the value changes. That gap has a name — it's called a parameter. And when you use the recipe, you fill the gap.

The example below shows it line by line. Read it all the way through before you type anything yourself.

04

Vorhersage — was passiert hier?

Erst antworten, dann ausführen. Drei Minuten, und es ist das billigste wirksame Ding auf dieser Seite.

  1. netto = 84.03; netto

  2. netto = 100; netto * 1.19

  3. netto_zu_brutto(100)

  4. netto_zu_brutto(100) + 1

05

Drill

Erst vorgemacht

Hier ist eine vollständige, fertige Funktion. Sie funktioniert. Du musst nichts daran tun — nur verstehen, was jedes Stück macht.

def netto_zu_brutto(netto):
    return netto * 1.19

Zwei Zeilen. Jetzt jedes Stück einzeln:

StückWas es bedeutet
def„Ich definiere jetzt etwas Neues." Das Wort steht immer am Anfang.
netto_zu_bruttoDer Name, den ich mir ausgedacht habe. Wie bei x = 5 — nur dass hier ein Rezept den Namen bekommt, kein Wert.
(netto)Die Lücke im Rezept, der Parameter. netto ist ein Name für „der Wert, der später hier hineinkommt".
:Der Doppelpunkt sagt: „jetzt kommt, was das Rezept tut."
die 4 LeerzeichenDie Einrückung. Sie sagt Python: diese Zeile gehört in die Funktion. Ohne sie ist es keine.
return„Gib das zurück." Das Ergebnis, das herauskommt.
netto * 1.19Der eigentliche Rechenweg, geschrieben mit dem Platzhalter statt mit einer festen Zahl.

Und so benutzt man sie

Das Rezept aufschreiben passiert einmal. Benutzen kannst du es beliebig oft — das heisst „die Funktion aufrufen":

netto_zu_brutto(84.03)

Was dabei in Python passiert, in Zeitlupe:

1. Python sieht den Namen und sucht das Rezept. 2. Der Parameter netto wird 84.03. (Genau wie netto = 84.03 — nur automatisch.) 3. Es rechnet 84.03 * 1.19. 4. return gibt das Ergebnis heraus: 99.9957

Und weil die Lücke ein Parameter ist, geht jeder andere Wert genauso:

netto_zu_brutto(12.50)   #  ->  14.875
netto_zu_brutto(7.99)    #  ->  9.5081

Jetzt du. Der Drill unten hat genau dieselbe Form — nur ein anderer Rechenweg.

Here is a complete, finished function. It works. You don't have to do anything to it — just understand what each piece does.

def netto_zu_brutto(netto):
    return netto * 1.19

Two lines. Now each piece on its own:

PieceWhat it means
def"I am now defining something new." This word always comes first.
netto_zu_bruttoThe name I made up. Like x = 5 — except here a recipe gets the name, not a value.
(netto)The gap in the recipe, the parameter. netto is a name for "the value that will come in here later".
:The colon says: "here comes what the recipe does."
the 4 spacesThe indentation. It tells Python this line belongs inside the function. Without it, it isn't one.
return"Give this back." The result that comes out.
netto * 1.19The actual calculation, written with the placeholder instead of a fixed number.

And this is how you use it

Writing the recipe down happens once. Using it can happen as often as you like — that is called calling the function:

netto_zu_brutto(84.03)

What Python does, in slow motion:

1. Python sees the name and looks up the recipe. 2. The parameter netto becomes 84.03. (Just like netto = 84.03 — only automatically.) 3. It calculates 84.03 * 1.19. 4. return hands the result out: 99.9957

And because the gap is a parameter, any other value works exactly the same:

netto_zu_brutto(12.50)   #  ->  14.875
netto_zu_brutto(7.99)    #  ->  9.5081

Now you. The drill below has exactly the same shape — just a different calculation.

✋ VON HANDKein AI, kein Autocomplete. Tippen.

Jeder Hinweis wird im Ledger vermerkt. Einen vierten gibt es nicht.

Die nächsten Abschnitte erscheinen, wenn alle Fälle grün sind.