Python Introduction to Functions

In the last lesson, we learned about Python Loop Patterns. Now let’s group repeated steps into reusable blocks instead of copying the same code.

⚙️ Let’s Talk About Functions

We have been writing code for a while now. We know about variables, loops, conditions, and all that.

But have we noticed something? Sometimes we write the same code again and again.

Maybe we calculate the area of a circle in one place. Then later, we need to calculate it again. So we copy the same lines. Then again somewhere else.

That is not good. It is repetitive. And if we make a mistake, we have to fix it in many places.

That is where functions come in.

🧩 What Is a Function in Python?

A function is a block of code that does a specific job. We give it a name. And whenever we need that job done, we just call the function by its name.

Think of it like a recipe. We write down how to make a cake once. Then whenever we want a cake, we just follow the recipe. We do not have to invent the recipe again each time.

A function is like that recipe. We write it once. We use it many times.

💡 Why Do We Need Functions in Python?

There are three big reasons.

First, they reduce repetition. We write the code once. We use it everywhere. If we need to change something, we change it in one place.

Second, they make our code easier to read. Instead of a long list of instructions, we have small, named blocks. calculate_area(), print_message(), save_data(). That is much easier to understand.

Third, they let us break a big problem into smaller pieces. Instead of one giant program, we have many small functions. Each one does one thing. That is much easier to manage.

🧪 A Simple Function Example in Python

Let’s say we want to greet a user.

Without a function:

print("Hello, Alex!")
print("Welcome to the program.")
print("We are happy to have you here.")

Now if we want to greet another user, we copy those three lines and change the name.

With a function:

def greet(name):
print(f"Hello, {name}!")
print("Welcome to the program.")
print("We are happy to have you here.")
greet("Alex")
greet("Sam")

Output

Hello, Alex!
Welcome to the program.
We are happy to have you here.
Hello, Sam!
Welcome to the program.
We are happy to have you here.

See? We wrote the greeting code once. Then we called it twice with different names.

That is the power of functions.

🔧 How to Define a Function in Python

We define a function using the def keyword. Then the function name. Then parentheses. Then a colon. Then the indented code.

def function_name():
# code goes here

Let me show a real one.

def say_hello():
print("Hello there!")
say_hello()

Output

Hello there!

Here, say_hello is the function. We defined it with def. Inside, it just prints a message. Then we called it by writing say_hello().

The parentheses are important. They tell Python to run the function. If we just write say_hello without parentheses, Python will not run it. It will just refer to the function object.

📞 Calling a Function Multiple Times in Python

We can call a function as many times as we want.

def say_hello():
print("Hello there!")
say_hello()
say_hello()
say_hello()

Output

Hello there!
Hello there!
Hello there!

That is it. We write it once, and use it many times.

🔍 Functions with Inputs in Python

The real power comes when we give the function some information to work with. That is called an argument or parameter.

def greet(name):
print(f"Hello, {name}!")
greet("Alex")
greet("Sam")

Output

Hello, Alex!
Hello, Sam!

Here, name is the parameter. When we call greet("Alex"), the value "Alex" is passed into the function. And inside, name becomes "Alex".

That is how we make functions flexible.

🧭 Python Functions: A Real-Life Example

Let’s write a function that calculates the area of a rectangle.

def area(length, width):
result = length * width
print(f"The area is {result}")
area(5, 3)
area(10, 4)

Output

The area is 15
The area is 40

We can reuse this function whenever we need to calculate an area. Just call it with the right numbers.

⚠️ Functions Make Code Cleaner

Imagine we have a long program. It does many things: calculates, prints, saves, etc.

Without functions, it is just a long list of instructions. Hard to read. Hard to debug.

With functions, we can group related instructions together. Give them a name. Then use that name to run the group. The main part of our program becomes just a few lines calling the functions.

That is why functions are so important. They bring order to chaos.

🎯 A Small Note

Functions can also return values. That is when they give something back to the caller. We will learn about that in a later article. For now, let’s just know that functions can print things, do calculations, and more.

Next, we will learn about Defining Python Functions and write our own reusable blocks of code.