Command Palette

Search for a command to run...

Hectal
Phase 5Beginner6 of 18 in Python

Functions

Name a group of steps and reuse it: parameters, defaults, *args and **kwargs, scope, closures, recursion, type hints, and how to design a good function.

A function is a named group of steps that you can run again and again with different inputs. Until now every program in this course was a list of statements. From this phase on, your programs are built from small, named, reusable pieces, which is how every real Python program is written.

You will learn how to define and call functions, how Python matches arguments to parameters (positional, keyword, default, variadic, keyword-only and positional-only), which names a function can see (scope), and why Python lets you treat a function as a value you can pass around, store in a dictionary, or return from another function. That last idea powers closures, sorting with a key, and the tool systems that let AI models call your code.

The phase ends with recursion (a function that calls itself), a first look at docstrings and type hints, and the design habits that make functions easy to read, test and trust. Every later phase assumes you are comfortable here.

0/10 · 0%
10 topics ~4 h 33 code blocks & diagrams
Start with the first topic
1
5.1

Defining and Calling Functions

A function is a named block of code that you define once with def and run whenever you call it by name. It can receive values (parameters), and hand a value back to the caller with return; if it never reaches a return, it gives back None.

28 min 1 diagram 2 code practice

2
5.2

Positional, Keyword and Default Arguments

Python can match call arguments to parameters by position, by name (keyword arguments), or fill in missing ones from defaults. Defaults are evaluated once when the function is defined, which makes a mutable default such as [] a classic trap.

26 min 1 diagram 3 code practice

3
5.3

*args, **kwargs and Unpacking in Calls

*args lets a function accept any number of positional arguments (collected into a tuple), and **kwargs any number of keyword arguments (collected into a dict). The same stars in a call do the opposite: they spread a list or dict into separate arguments, which is how wrappers forward arguments unchanged.

24 min 3 code practice

4
5.4

Keyword-Only and Positional-Only Parameters

A bare * in a signature makes every parameter after it keyword-only, and a / makes every parameter before it positional-only (/ arrived in Python 3.8). They let a function's author decide how callers may pass each argument, which keeps APIs readable and flexible to change.

24 min 3 code practice

5
5.5

Scope and Namespaces: LEGB

Scope decides which names a piece of code can see. Python looks a name up in four places in order: Local, Enclosing, Global, Built-in (LEGB). Assigning to a name inside a function makes it local unless you declare it global or nonlocal.

26 min 1 diagram 2 code practice

6
5.6

Functions Are Values: Passing Them Around

In Python a function is an object like any other: you can store it in a variable, put it in a list or dict, pass it to another function, and return it. That idea powers sorted(key=...), map, filter, small lambda functions, and the tool tables that let AI models call your code.

26 min 3 code practice

7
5.7

Closures

A closure is a function that remembers the variables of the function it was created in, even after that outer function has returned. Closures make counters, function factories and private state possible, and they have one famous trap: late binding in loops.

25 min 1 diagram 3 code practice

8
5.8

Recursion

A recursive function solves a problem by calling itself on a smaller version of the same problem, until it reaches a base case it can answer directly. Python allows it, but caps the depth (about 1000 calls by default) and has no tail-call optimisation, so deep problems are usually written as loops.

28 min 4 code practice

9
5.9

Docstrings and Type Hints: A First Look

A docstring documents what a function does, and type hints (annotations) document what types its parameters and result should be. Python stores both on the function, shows them in help(), and lets tools read them, but it does not enforce the hints when the code runs.

25 min 3 code practice

10
5.10

Designing Good Functions

A good function does one job, has a clear name and a small signature, returns its result instead of printing it, avoids changing its inputs or hidden state, and uses early returns to keep the main path flat. These habits make code easy to read, test and reuse.

27 min 3 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.