In this lesson you will
- Write functions with def, return, default and keyword arguments
- Return several values and explain local scope
- Import and use standard library modules such as math and datetime
def, arguments and return
A function starts with def, its name, the arguments in brackets and a colon, and its body is indented like an if block. Defining it does nothing visible; the code runs only when you call it with brackets, as in vat(200). return sends a value back to the caller and ends the function at once, so any line after it in the same branch never runs. The receipt helper calls vat() inside itself, which is how bigger programs are built: small functions that each do one job, combined into larger ones.
An argument with a default, such as rate=0.05, can be left out, and the caller can still pass a different value by name: vat(200, rate=0.15) gave 30.0 instead of 10.0. Arguments with defaults must come after those without. Passing by name also makes calls easier to read, which is why qty=2 is clearer than a bare 2. The last two lines show a trap: print() displays text but returns None, so result held None. A function that only prints cannot be used in a calculation; return the value and print it where you call it.
Console
- Console messages, errors, form data and clicked links appear here.
This printed 10.0, 30.0, Dates x2: 94.50 AED and Oud oil x1: 126.00 AED, then hello and None. Change the default to rate=0.15 and every call that left the rate out changes with it.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 2 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between a parameter and an argument?
The names in the def line, such as amount and rate, are parameters. The values you pass in a call, such as 200, are arguments. Many people use the words interchangeably, and Python error messages say argument.
Should I add type hints to my functions?
They are optional and Python does not enforce them when the code runs. Writing def vat(amount: float) -> float: documents what goes in and out, and editors and tools such as mypy use the hints to catch mistakes early.
How do I see what a module offers?
Run print(dir(math)) for a list of names, or help(math.ceil) for the documentation of one function. The Python Standard Library pages on docs.python.org describe every module with examples.
What does a docstring do?
A string in triple quotes on the first line of a function body describes what it does. help() and editors show it, so write one short sentence for any function that someone else will call.
Can a function call itself?
Yes, that is recursion, and it suits nested data such as folder trees. Python stops after about 1,000 nested calls with a RecursionError, so a plain loop is usually better for long counts.
Sources
Facts in this lesson were checked against these official pages on 3 December 2026.
- Python docs: Defining Functions
- Python docs: Modules
- Python docs: The Python Standard Library
- Python docs: datetime
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.