Python Day #8: Breaking Code into Small Functions Makes You a Better Developer
Discover why splitting your Python code into small, clear functions with type hints is the key to cleaner, easier-to-read, and maintainable code that grows with you.
You now have the skills to build reusable blocks of code that you define once and get to use anytime, anywhere. This is the essence of Functions.
You now need the chance to practice and grasp this topic as we begin to ramp things up. Because functions are doing so much more than just getting called one by one.
When we write good code, we want functions to work together. The trick here is to get functions to utilize other functions. When we run our scripts, one block should be able to control and call the rest of out code.
Welcome back to the Python Roadmap to Success, where I am giving all my readers a fair chance at learning Python the right way, following a proven roadmap that I’ve structured and taught for over the last four years.
In our last article, we covered the seventh phase in the Roadmap to Success, it covered How to Create Functions in Python. This lesson will help you reinforce everything you’ve been learning, while giving you time to digest what we covered.
In this lesson, we are going to enhance your understanding of defining functions. This is a core Python fundamental that you will inevitably use to write scripts. So breaking this down to a level you can understand is important to me.
Every week you’ll be introduced to a new topic in Python, think of this as a mini starter course to get you going and allow you to have a structured roadmap that actually builds to create you a solid foundation in Python. Join us today!
Functions help you keep your code clean, organized, and reusable. Every function you write should do one specific thing. That’s it, now how do we get functions to engage with each other?
Over the last 8+ years I’ve been teaching full-time around the world which has given me the unique opportunity to work with over 1,500+ students. The last four years I’ve been teaching Python full-time. Breaking down complex topics, step by step.
I’ve spent this time curating a structured learning path that ensures my students have the greatest chance of success and don’t just learn but actually master Python as well as their logical thinking and problem solving.
In this article, we’re going to build on what you’ve already learned. Last time, you worked on defining your own functions. Now, it’s time to practice and build on that. The goal is to have you start writing code that interacts together.
One point I should note is, spend time digesting these when I write them, after six weeks most of my posts are automatically put behind a paywall.
If you haven’t subscribed to my premium content yet, you should definitely check it out. You unlock exclusive access to all of these articles and all the code that comes with them, so you can follow along!
Plus, you’ll get access to so much more, like monthly Python projects, in-depth weekly articles, the '3 Randoms' series, and my complete archive!
I spend a lot of my week on these articles, so if you find it valuable, consider joining premium. It really helps me keep going and lets me know you’re getting something out of my work!
Thank you for allowing me to do work that I find meaningful. This is my full-time job so I hope you will support my work by joining as a premium reader today.
👉Only this next month for Mothers & Fathers Day, I am offering my readers a 30% off on all annual subscriptions! Show your parents you care about your career, thanks mom and dad…
If you’re already a premium reader, thank you from the bottom of my heart! You can leave feedback and recommend topics and projects at the bottom of all my articles.
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
P.S - Drop any questions in the comments or feel free to DM me directly as I’m always happy to help you guys out.
Quick Review of Functions
Last lesson I set the stage for how you can define your own functions. You were introduced to the keyword def
, this just means define and we use this at the start when we are going to define/declare a new function in our code.
After def
, we can name the function, that’s then followed by a set of parentheses. You can name you function anything, but the same naming conventions apply that you used for variables. No capitalization, no spaces, don’t start with numbers, etc.
Inside the parentheses you can put any parameters that the function will use. A parameter is just a variable that you will use in the function. The value to the parameters are arguments. We give arguments when you call the function.
Indentation is also very important here, as the next line should be indented to tell Python that this is the code that’s wrapped in the function. Here is the example I started you guys off with last lesson.
Above you can see I defined my own function, check_even_numbers
. I then gave this two parameters, num1
and num2
. Currently these parameters have no value, but we can write the function as if they already do, think ahead.
All this function does is check if added together, these parameters are an even or odd number, then based on this it will return True
or False
. The key thing here is to remember a function returns a value.
When a function returns a value, we can store that value in a variable. So the value to the variable results
is either going to be True
or False
, which ever the function returns to us.
We can then use that variable results in other parts of our code as it now has a value. Yes, you can have your function print
off things, but you can not use a printed value, this is just for you.
A function should most of the time return a value that you can then use at another part in your code. Speaking of that…
👉 I genuinely hope you get value from these articles, if you do, please help me out, leave it a ❤️, and share it with others who would enjoy this. Thank you so much!
Functions that work together
When we write a Python script, you’ve probably seen at the bottom of the main.py
file there is more often than not a main()
function of sorts. When the script is ran, this is usually the only function that is executed directly.
The main function often calls other blocks of your code, then these blocks call other blocks. You can think of this kind of like a pyramid. The top of the pyramid is the main function, then each level calls and interacts with other blocks of code.
When you run a Python script directly, every line in that file runs from top to bottom. Sometimes you might only want some code to run right away (like starting your app), while other code (like functions or classes) should only run when called later.
The purpose of this is to help organize your code, make it easier to reuse or import into other scripts, and give you more control to what runs when you run the script vs. when you import it.
Now in my above code I don’t have a main()
function but there is a “parent/controller” function which is calling other parts of our code. This is the calculate_total_bill
.
A key point is to remember that a function should serve one specific purpose, this makes the code more reusable and gives you the chance to use it in other parts of your programs. Yes, this could be done in once function, but that makes it less reusable so we want to avoid this when we can.
You can see the only function this I directly call when I run my code is, calculate_total_bill
. Once this function is called, it runs. When this runs it then calls the other two functions, calculate_tax
and calculate_tip
.
This collects the return values of both of those functions, this uses these to calculate the total bill cost. The final return value from this function is what it gets when it’s all done.
I’m creating interaction between functions above while I aimed to use some Python best practices. First, each function serves a specific purpose, second you can see in my code that I am using type hintings, which is something we covered in our last lesson. Below I will cover why we do all this for you guys!
Ready to Take Your First Steps with Python?
Most people get stuck before they even start. But you don’t have to!
I’ve put together a Free Course: The Pure Basics of Python that covers the very basics of Python, made just for beginners like you.
👉 I’m giving you my exact system that’s been proven and tested by over 1,500 students over the last 4+ years!
Here’s what you’ll learn:
✅ Step-by-step help to set up VS Code and install Python.
✅ Learn how to use variables, built-in functions, and even nesting functions.
✅ A quick test to see how much you’ve picked up.
✅ My handcrafted Python Guide to keep you on track.
This free course is a perfect first step — you’ll get a feel for my teaching style and build confidence to keep going.
👉 Ready to get started?
P.S. — If you like the free course (and I think you will!), I’ll show you how to dive deeper with my full Python Masterclass.
🎁 - Get a free one-on-one coaching session with any Masterclass!
Go through the entire Masterclass, complete all material, and attend the Q&A's, if you still feel like your struggling I'll personally work with you one-on-one until you're confident!
Why We Write Code Like This
You might be asking yourself why I went through the trouble of breaking the calculation into three separate functions and why I added type hints. Let me touch on this, as this isn’t just some fancy coding trick. It’s actually one of the best ways to write code.
1. It’s Easier to Read and Keep Up With
When you break your code into small, clearly named functions that each do one thing, it’s way easier to read and keep organized. This means fewer mistakes and it’s easier to fix problems because you can test each function on its own.
2. You Can Use It Again
Small, focused functions are like little building blocks. Once you write and test them, you can use them in other projects. This saves you time because you’re not rewriting the same thing over and over again.
3. Keeps Things Separate
Each function in your code should only have one job. This is called “separation of concerns,” and it’s a big deal in writing clean code. By splitting things up into simple, single-purpose functions, you make sure that changing one part of your code doesn’t mess up something else.
4. Type Hints Make Things Clear
This makes it easier for people (and computers) to understand your code and helps you catch mistakes early. Plus, type hints work with code editors that give you suggestions or highlight errors, which helps you spot problems before they get bigger.
5. Everything Works Together
Finally, by having a main function like calculate_total_bill()
, you set up a clear order for your code to follow. This makes your code easier to read and test. You can test each small function separately, and then test the main one to see how everything fits together.
My Best Starter Resources
Here are the best resources I have to offer to get you started with Python no matter your background! Check these out as they’re bound to maximize your growth in the field.
Code with Josh: This is my YouTube channel where I post videos every week designed to help break things down and help you grow.
Zero to Knowing: My Python Masterclass platform designed from 8 years teaching experience and that has helped over 1,500+ students succeed with Python. (All my readers unlock a limited time, 35% discount, use code: pythonsummer35)
My Books: Maybe you’re looking to get a bit more advanced in Python. I’ve written 3 books to help with that, from Data Analytics, to SQL all the way to Machine Learning.
The Nerd Nook: This is where you are right now! I write here multiple times per week with the sole goal of breaking down this larger than life topic to help you excel and learn Python faster than you thought was possible.
This article will be a piece to a larger roadmap but will go behind a paywall after 6 weeks! To help support my work in teaching others consider joining as a premium reader to unlock all these articles, monthly Python projects and in-depth dives.
Wrapping Up Lesson Eight
Alright, to wrap this all up, breaking your code into small, focused functions—and adding type hints—makes it way easier to read, test, and keep up with.
It cuts down on mistakes and saves you time when you come back to it later or share it with someone else. When you structure your code like this, you’re not just making it work right now—you’re setting yourself up for success down the road.
That’s what makes a great programmer stand out from the rest. Keep at it, and you’ll see how much smoother coding becomes.
Let’s keep moving forward with Day #9 next week.
Hope you all have an amazing week nerds ~ Josh (Chief Nerd Officer 🤓)
👉 If you’ve been enjoying these lessons, consider subscribing to the premium version. You’ll get full access to all my past and future articles, all the code examples, extra Python projects, and more.