Drop #10 – Writing Your Own Functions

Drop #10 – Writing Your Own Functions

Have you ever copied and pasted the same few lines of code multiple times?

Or changed one line in five places just to fix a bug?

That’s your cue: it’s time to write your own functions.

In Python, functions are the building blocks of clean, reusable, and testable code.


🛠️ Think of functions like tools

Imagine you're baking bread. You follow a recipe:

1. Add flour, water, yeast, salt.

2. Knead the dough.

3. Let it rise.

4. Bake it.

Now imagine coding this as a function:

With this function, you can now "bake bread" with any ammount of ingredients you want, without rewriting the process each time.


🔍 How to write a function in Python

Here’s the basic syntax:

Let’s say we want to calculate the area of a rectangle:

Now, instead of repeating in your code, you just call:

Simple, readable, and reusable.


🎯 Why return matters

The keyword sends a result back to wherever the function was called.

If you don’t return anything, the function still runs—but it returns by default.

Use when you want your function to produce a value you can reuse.


🚨 Common mistakes to avoid

  • Forgetting to use when needed

  • Using the same name for parameters and variables, causing confusion

  • Writing functions that do too much — keep them focused


✅ Pro tip: Functions make your code testable

When logic is wrapped in functions, you can write unit tests for them in isolation — no UI, no manual steps, just pure logic. It’s a cornerstone of professional Python development.


Next up: – Python’s built-in way to store structured, labeled data.


This post is part of my ongoing Python Drops series.

Clear. Practical. Professional.

Helping you master Python — one drop at a time.


Tell me: What’s a small function you wrote recently that made your life easier?

Adilton Seixas

Senior Software Engineer | PHP | Laravel | Vue.js

2w

Well put, Arthur

Like
Reply
Claudeci Lucena

Software Engineer | Java | Spring | Microservices | AWS | 15+ years of experience | Remote-ready

2w

Thanks for sharing, Arthur

Like
Reply
Julio César

Senior Software Engineer | Java | Spring Boot | AWS | React | Angular | LLM | GenAI | CI/CD | MySQL | MongoDB | JUnit | Mockito | APIs

2w

Well put, Arthur

Like
Reply
Otávio Prado

Senior Business Analyst | Agile & Waterfall | Data Analysis & Visualization | BPM | Requirements | ITIL | Jira | Communication | Problem Solving

2w

Great guide Arthur Faria ! Thank you! 💯🚀

Like
Reply
Danilo Queiroz

Fullstack Software Engineer | Java | Spring Boot | EJB | JSF | Angular | React | AWS Services | Azure Pipeline | Docker | Node | Jasper Reports

2w

Nice

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore topics