Vom Wert zur Funktion
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.
netto = 84.03; nettonetto = 100; netto * 1.19netto_zu_brutto(100)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ück | Was es bedeutet |
|---|---|
def | „Ich definiere jetzt etwas Neues." Das Wort steht immer am Anfang. |
netto_zu_brutto | Der 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 Leerzeichen | Die 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.19 | Der 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:
| Piece | What it means |
|---|---|
def | "I am now defining something new." This word always comes first. |
netto_zu_brutto | The 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 spaces | The 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.19 | The 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.
- Fang mit dem Wort
defan, dann der Name, dann die Klammern mit dem Platzhalter darin, dann ein Doppelpunkt. - Zeile 1 ist
def rabatt_10(preis):— Zeile 2 ist eingerückt und fängt mitreturnan. - Dieselbe Form auf anderen Daten:
def haelfte(betrag): return betrag * 0.5— genau so, nur mit 0.9 statt 0.5.
Jeder Hinweis wird im Ledger vermerkt. Einen vierten gibt es nicht.
Die nächsten Abschnitte erscheinen, wenn alle Fälle grün sind.
06
Die Entscheidung
Das ist deine erste Architektur-Entscheidung. Sie sieht winzig aus und ist die Grundform von allem, was in diesem Kurs noch kommt.
Die Anforderung: Rechne Netto in Brutto um.
Entwurf A — die Zahl steht im Code
def brutto():
return 84.03 * 1.19
Entwurf B — der Wert kommt von aussen
def brutto(netto):
return netto * 1.19
Der Tradeoff — fülle das aus, bevor du weiterliest
| Entwurf A | Entwurf B | |
|---|---|---|
| Was es kauft | ||
| Was es kostet | ||
| Wann es gewinnt |
Die Frage
> Du bekommst 400 Rechnungszeilen mit 400 verschiedenen Netto-Beträgen. Wie viele > Funktionen brauchst du in Entwurf A, und wie viele in Entwurf B?
Schreib die zwei Zahlen hin, bevor du weiterscrollst.
Was daran Architektur ist
In A ist der Wert eingebacken. Die Funktion kann genau eine Sache und du brauchst 400 davon. In B ist der Wert ein Parameter, und eine Funktion reicht für alle 400.
Der Unterschied hat einen Namen, und du wirst ihm den ganzen Kurs über wieder begegnen:
In Modul 03 kommt genau dieselbe Entscheidung noch einmal — dann geht es nicht mehr um einen Betrag, sondern um die Rundungsregel, die genauso eingebacken oder genauso übergeben werden kann. Wenn du heute verstehst, warum B gewinnt, verstehst du Modul 03 schon zur Hälfte.
This is your first architecture decision. It looks tiny and it is the basic form of everything else in this course.
The requirement: convert net to gross.
Design A — the number sits in the code
def brutto():
return 84.03 * 1.19
Design B — the value comes from outside
def brutto(netto):
return netto * 1.19
The tradeoff — fill this in before you read on
| Design A | Design B | |
|---|---|---|
| What it buys | ||
| What it costs | ||
| When it wins |
The question
> You get 400 invoice lines with 400 different net amounts. How many functions do you > need in Design A, and how many in Design B?
Write down the two numbers before scrolling on.
What is architectural about this
In A the value is baked in. The function can do exactly one thing and you need 400 of them. In B the value is a parameter, and one function covers all 400.
The difference has a name, and you will meet it throughout this course:
Module 03 brings exactly this same decision back — but there it isn't an amount, it's the rounding rule, which can be baked in or passed in just the same way. If you understand today why B wins, you already understand half of module 03.
10
Die Meisterprüfung
a) Erklären
Ein deutscher Satz, maximal 25 Wörter. Python-Konstrukte zu nennen ist verboten — kein „for-Schleife“, kein „dict“, kein „try/except“. Sag, was der Code tut, nicht wie er aussieht.
b) Änderungs-Folgen
Eine Anforderung ändert sich. Antworte bevor du etwas ausprobierst — es geht um die Vorhersage, nicht um das Ergebnis.
Die Änderung
> Der Rabatt ist ab morgen nicht mehr immer 10 %. Für Grosskunden sind es 15 %, für > Neukunden 5 %, und der Vertrieb will das pro Kunde einstellen können.
Die Fragen
1. Deine Funktion rabatt_10(preis) hat die 0.9 im Code. Wie viele Funktionen bräuchtest du, wenn du so weitermachst — und wie heissen sie? 2. Schreib die Signatur (nur die erste Zeile, das def ...) einer Funktion hin, die alle drei Fälle kann. Wie viele Lücken hat sie? 3. Was ist dann noch im Code eingebacken, und ist das in Ordnung? 4. Und die eigentliche Frage: Wer entscheidet jetzt über den Rabattsatz — die Funktion oder der, der sie aufruft? Warum ist das eine Verbesserung und nicht bloss eine Verschiebung?
A requirement changes. Answer before you try anything — this is about the prediction, not the result.
The change
> From tomorrow the discount isn't always 10 %. Large accounts get 15 %, new customers 5 %, > and sales wants to set it per customer.
The questions
1. Your function rabatt_10(preis) has the 0.9 in the code. How many functions would you need if you keep going this way — and what are they called? 2. Write down the signature (just the first line, the def ...) of a function that handles all three cases. How many gaps does it have? 3. What is still baked into the code then, and is that all right? 4. And the real question: who decides the discount rate now — the function, or whoever calls it? Why is that an improvement and not merely a relocation?
11
Abschluss
Drei Felder, dann bist du fertig — und das nächste Modul beginnt mit dem, was du hier schreibst.
Danach im Terminal: uv run python -m werkbank.stand --nach 00, dann committen. Der Commit ist die Aufzeichnung — nicht dieser Browser.