Welcome to Part 4 of our series: Functions and Modularity! Until now, all of our programs have been written as single scripts that run from top to bottom. As our programs grow in complexity, this approach becomes difficult to manage. The key to writing clean, professional, and maintainable code is organization.
In this post, before we write any new syntax, we’re going to learn about the single most important principle that motivates this organization: DRY, or Don’t Repeat Yourself.
The Problem: “WET” Code
Let’s imagine we’re writing a script that needs to display a formatted welcome banner for a few different users. A beginner’s approach might be to write the logic and then copy and paste it for each user.
# --- For User 1 ---
user_name_1 = "Alice"
print("--------------------")
print(f"Welcome, {user_name_1}!")
print("Have a great day!")
print("--------------------")
# --- For User 2 ---
user_name_2 = "Bob"
print("--------------------")
print(f"Welcome, {user_name_2}!")
print("Have a great day!")
print("--------------------")
This code works, but it’s what programmers call “WET”—a tongue-in-cheek acronym that can stand for “Write Everything Twice” or “We Enjoy Typing.” This approach has serious downsides that can lead to major headaches.
The Dangers of Repetition
Copying and pasting code is a recipe for future problems.
- It’s Hard to Maintain: What if you want to change the welcome message from “Welcome” to “Greetings”? You would have to hunt down every single place you copied the code and change it. If you miss one, you’ve introduced a bug and inconsistent behavior in your program.
- It’s Error-Prone: The more you copy and paste, the higher the chance of making a small mistake in one of the copies. These bugs can be very hard to track down because the code looks almost identical.
- It’s Hard to Read: Large blocks of repeated code make your script longer and obscure the overall purpose of the program. It’s hard to see the “forest” (what your program is trying to achieve) for the “trees” (the endless repeated lines).
The Solution: The DRY Principle
DRY stands for Don’t Repeat Yourself. It is a fundamental principle in software development that states that every distinct piece of logic in your program should have one, and only one, representation.
Instead of copying and pasting the logic for our welcome banner, we should define it in a single, authoritative place. We should abstract it into a reusable block that we can call upon whenever we need it.
Introducing Functions: The Heart of DRY
This is the core motivation for learning about functions. A function is a named, reusable block of code that performs a specific task.
With functions, we can write our welcome banner logic just once and give it a name, like display_welcome_banner
. Then, anytime we need to display that message, we can simply “call” the function by its name. If we ever need to update the banner—to change the text or the border—we only have to change it in that one single location. The change will automatically apply everywhere the function is called, eliminating the risk of inconsistency.
What’s Next?
The DRY principle is a guiding star for writing good code. By avoiding repetition, you create programs that are easier to read, easier to debug, and easier to maintain. Functions are the primary tool we use to achieve this.
Now that we understand why we need to avoid repetition, it’s time to learn how. In Post #84, we will learn the syntax for defining and calling your very first reusable function in Python.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com