Elena' s AI Blog

Python Functions: Writing Reusable Code

12 May 2026 (updated: 17 Aug 2026) / 18 minutes to read

Elena Daehnhardt


Generated by Midjourney. Prompt: A very smart parrot writing Python code — humorous developer mascot.


TL;DR:
  • Functions let you write code once and reuse it everywhere. Learn def, parameters, return values, default arguments, *args/**kwargs, type hints, and lambda expressions — the tools that make your Python programs clean, readable, and maintainable.

Previous: Part 1 — Python Programming Language

Next: Part 3 — Python Error Handling: When Birds Misbehave

Python Functions: Why Reusable Code Matters

In my first Python post we covered variables, lists, dictionaries, and list comprehensions — the data and control flow that let you write a working script. In the OOP post we jumped all the way to classes. But there is an important stop in between, and that stop is functions.

Functions are how you stop writing the same thing twice. Functions are the reason a 500-line program does not become a 5,000-line program, and they represent the first step toward thinking about code as something you design rather than something you just write. Once functions feel natural, classes make much more sense — a class is largely just a collection of functions that share some data.

The birds stay in this post too — patient, useful, and by now familiar.

What Is a Function in Python?

A function is a named, reusable block of code that you can call by name, pass data into, and get a result back from. In Python you define one with def:

def greet_bird(name):
    print(f"Hello, {name}!")

greet_bird("Eagle")
greet_bird("Pigeon")
Hello, Eagle!
Hello, Pigeon!

Writing the logic once inside def and calling it as many times as needed is the whole idea behind functions. Without functions we would have to repeat print(f"Hello, ...") every time — tedious, error-prone, and hard to change later.

Python Function Parameters and Return Values

🔒 Subscribe to keep reading.

Python Default Arguments and the Mutable Default Gotcha

🔒 Subscribe to keep reading.

Python *args and **kwargs: Variable-Length Arguments

🔒 Subscribe to keep reading.

Python Type Hints for Function Parameters and Return Values

🔒 Subscribe to keep reading.

Python Lambda Functions: Anonymous Single-Expression Functions

🔒 Subscribe to keep reading.

Combining Parameters, *args, and **kwargs: A Bird Feeding Station Example

🔒 Subscribe to keep reading.

Python Functions: Key Takeaways

🔒 Subscribe to keep reading.

References

🔒 Subscribe to keep reading.

Subscribe to unlock the full article ❤️

The form below signs you up for the newsletter. It does not log you into the app — log in afterwards (same email) to unlock this article and download your subscriber gifts. New subscribers get an inbox mail: Set a password to unlock articles.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2026) 'Python Functions: Writing Reusable Code', daehnhardt.com, 12 May 2026. Available at: https://daehnhardt.com/blog/2026/05/12/python-functions/
All Posts