Skip to content

Python Functions and Modules: def, return, Default Arguments, Scope and import

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Once a calculation appears in two places, it belongs in a function. A function gives a piece of logic a name, takes inputs called arguments, and hands back a result with return, so you write the VAT rule once and use it everywhere. Modules go one step further: they are files of ready-made functions that you load with import, and Python ships with hundreds of them for maths, dates, statistics and much more. This lesson writes a VAT and receipt helper, returns several values at once, sorts products by price with a short lambda, shows why a variable inside a function does not touch one outside it, and uses four standard library modules. Every example runs in your browser.

Quick answer

Define a function with def name(arguments): and an indented body, and send a value back with return; without it the function returns None. Give arguments defaults with rate=0.05, and pass them by name with vat(200, rate=0.15). Return several values as a tuple and unpack them with a, b = f(). Variables created inside a function are local to it. Load a module with import math or from datetime import date, then call its functions such as math.ceil().

40 min read and practice · Content checked on 3 December 2026

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.

Try it: A VAT helper and a receipt line
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.