Have you ever found yourself writing the same chunk of code over and over again in your Python programs? Maybe you’re calculating the same thing, checking the same condition, or printing the same formatted message multiple times. Not only is this tedious, but it also makes your code longer, harder to read, and a nightmare to update.

What if you could write that code just once and use it as many times as you want? That’s the magic of functions. In this guide, you’ll learn what functions are, why they are essential, and how to create your own. By the end, you’ll be able to package your code into reusable blocks, making you a more efficient programmer.
What is a Function? The “Black Box” Analogy
In the simplest terms, a function is a reusable block of code that performs a specific task. Think of it as a “black box.”
Imagine you’re a chef. You need chopped onions for several dishes. You don’t start from scratch—peeling, washing, and chopping—every single time you need onions. Instead, you prepare a bowl of chopped onions once and use a scoop whenever a recipe calls for it.
A function works the same way:
- You write the code once (preparing the chopped onions).
- You give it a name (like
chop_onions). - Whenever you need that task performed, you “call” the function by its name (scoop from the bowl).
You don’t necessarily need to know the intricate details of how the function works internally every time you use it. You just need to know what it does and how to use it.
You’ve Already Used Functions!
If this sounds familiar, it’s because you’ve been using a function from the very start: the print() function.
print("Hello, World!")You know that when you write print("something"), it displays “something” on the screen. You don’t need to understand the complex code inside Python that makes this happen. You just provide the input (the text) and get the output (the text on your screen). print() is a pre-made, built-in function.
The Problem: Repetitive Code
Let’s look at the problem from the video. We need to check if multiple numbers are even or odd. Without functions, our code looks messy and repetitive.
# Checking for the number 5
number = 5
if number % 2 == 0:
print("This is an even number")
else:
print("This is an odd number")
# Checking for the number 6
number = 6
if number % 2 == 0:
print("This is an even number")
else:
print("This is an odd number")
# Checking for the number 16
number = 16
if number % 2 == 0:
print("This is an even number")
else:
print("This is an odd number")The Issues:
- Readability: The code is cluttered. The core logic is duplicated, making it harder to understand the program’s flow.
- Maintenance: What if you need to change the output message from “This is an even number” to “EVEN”? You’d have to change it in three, four, or maybe ten different places, which is error-prone.
- Efficiency: You are writing more lines of code than necessary.
The Solution: Creating Your Own Function
Let’s package the even/odd checking logic into a function we can call check_even_odd.
How to Define a Function
You use the def keyword to define a function in Python.
# Defining the function
def check_even_odd(number):
"""Checks if a number is even or odd and prints the result."""
if number % 2 == 0:
print(f"{number} is an even number")
else:
print(f"{number} is an odd number")Let’s break this down:
def: The keyword that starts the function definition.check_even_odd: The name we chose for our function. Make it descriptive!(number): This is a parameter. It’s a variable that holds the value you “pass” into the function. It’s the input slot.:: The colon indicates the start of the function’s body (the code that will run).- The indented code block is the function’s body. This is the logic that runs when the function is called.
- The triple quotes (
"""...""") are a docstring, a brief comment explaining what the function does. This is a best practice!
How to Call a Function
Now, the magic happens. We can use this function multiple times without rewriting the logic.
# Calling the function with different inputs
check_even_odd(5)
check_even_odd(6)
check_even_odd(16)Output:
5 is an odd number 6 is an even number 16 is an even number
Look at how much cleaner and more readable that is! Each line clearly states its intent. If you need to check another number, you just add one more line: check_even_odd(42).
Level Up: Functions That Return Values
The check_even_odd function is great, but it only prints the result. Often, you want a function to calculate a value and give it back to you so you can use it later in your program. This is done using the return keyword.
Let’s improve our function.
# A function that returns a value
def is_even(number):
"""Returns True if the number is even, otherwise False."""
if number % 2 == 0:
return True
else:
return False
# Using the return value
result1 = is_even(10)
result2 = is_even(15)
print(f"Is 10 even? {result1}")
print(f"Is 15 even? {result2}")
# You can use the function directly in conditionals
if is_even(20):
print("20 is definitely even!")Output:
Is 10 even? True Is 15 even? False 20 is definitely even!
This is_even function is more powerful. Instead of just printing, it gives us a value (True or False) that we can store in a variable, print, or use in an if statement. This makes functions incredibly versatile.
Ready to Go from Basics to Pro?
You’ve just taken a crucial step from writing basic scripts to structuring real programs. Functions are the building blocks of all scalable Python applications, and mastering them is your gateway to tackling more complex projects like web development, data analysis, and automation.
If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.
Conclusion
Congratulations! You now understand the core concept of Python functions. You’ve learned how they help eliminate repetitive code, improve readability, and make your programs easier to maintain. You know how to define a function using def, provide it with parameters, and call it. You’ve also seen the power of the return statement for creating more flexible functions. Keep practicing by trying to turn your own repetitive code into functions. This is just the beginning of writing clean, efficient, and professional-level code!
Common Questions About Python Functions
Q: What’s the difference between a parameter and an argument?
A: A parameter is the variable listed inside the parentheses in the function definition (e.g., number in def is_even(number):). An argument is the actual value you pass to the function when you call it (e.g., 10 in is_even(10)). In simple terms, the parameter is the placeholder, and the argument is the actual value.
Q: How many values can a function return?
A: A function can return multiple values. In Python, you can use a comma-separated list, and it will be returned as a tuple. For example: return True, "It is even". You can then unpack these values when you call the function.
Q: Can a function call other functions?
A: Absolutely! This is a fundamental concept. You can call one function from inside another. For example, you could have a calculate_stats(data) function that calls your is_even(number) function multiple times as part of its logic.

















