Kahibaro
Discord Login Register

Why functions matter

Why we use functions at all

As programs get bigger, you quickly run into three problems:

  1. The code becomes hard to read.
  2. The code becomes hard to change.
  3. You keep writing the same code over and over.

Functions exist to solve these problems. A function is a named block of code that does one job. You can “call” it whenever you need that job done, instead of rewriting the same steps.

In this section you’ll focus on why functions are useful, not how to define them (that comes in later sections of this chapter).

Avoiding repetition (DRY: “Don’t Repeat Yourself”)

Without functions, you might write the same kind of code many times:

print("Hello, Alice!")
print("Welcome to the program.")
print("Have a great day!")
print("Hello, Bob!")
print("Welcome to the program.")
print("Have a great day!")
print("Hello, Carol!")
print("Welcome to the program.")
print("Have a great day!")

If you decide to change the message, you must find and modify every copy. That’s annoying and error-prone.

With a function, you write the steps once, then reuse them:

def greet_user(name):
    print("Hello,", name + "!")
    print("Welcome to the program.")
    print("Have a great day!")
greet_user("Alice")
greet_user("Bob")
greet_user("Carol")

Now if you change the greeting, you change it in one place. This is the core idea behind functions: one definition, many uses.

Breaking big problems into smaller pieces

Real-world tasks are usually too big to think about all at once. Functions let you split a big problem into smaller, named pieces.

Imagine you’re writing a program to manage a simple shop:

Trying to do all of this in one long sequence of code would be confusing. Instead, you might imagine pieces like:

Each function focuses on one job. You can then combine these small jobs to solve the big problem. This approach is often called decomposition: breaking a complex problem into simpler parts.

Benefits of decomposition with functions:

Making code easier to read and understand

Function names act like labels for ideas.

Compare:

# Version with no functions
total = 0
for price in prices:
    total = total + price
if total > 100:
    discount = total * 0.1
else:
    discount = 0
final_total = total - discount
print("Total after discount:", final_total)

vs:

def calculate_total(prices):
    total = 0
    for price in prices:
        total = total + price
    return total
def calculate_discount(total):
    if total > 100:
        return total * 0.1
    return 0
def calculate_final_total(prices):
    total = calculate_total(prices)
    discount = calculate_discount(total)
    return total - discount
final_total = calculate_final_total(prices)
print("Total after discount:", final_total)

The second version has more lines, but it’s often easier to understand because:

Functions turn messy details into clean, named steps. This is a key part of writing programs that humans can read and maintain.

Making changes safely

When logic is spread everywhere, small changes can break things in unexpected places.

With functions:

Example: suppose you decide to change your discount rule.

Without functions, you might have discount calculations copied in multiple places. You must find them all and update them; you might miss one.

With a function:

def calculate_discount(total):
    if total > 100:
        return total * 0.1  # 10% discount
    return 0

If you later decide:

you only edit those lines in calculate_discount. Every part of your program that calls calculate_discount now uses the new rules.

Functions help you:

Reusing logic across different programs

Sometimes you need the same kind of functionality in multiple programs.

For example, you might write code to:

If you put this logic into functions, you can:

  1. Use them multiple times within a single program.
  2. Copy or import them into other programs.
  3. Gradually build your own small “toolbox” of useful functions.

Functions are the first step toward creating your own reusable library of code.

Hiding complexity (abstraction)

When you call a function, you don’t have to know how it works internally—only what it does and how to call it. This idea is called abstraction.

Example:

result = sum([1, 2, 3, 4])

You probably don’t think about how sum is implemented. You just trust that:

Your own functions can work the same way. For example:

def is_valid_password(password):
    # imagine we check:
    # - length
    # - contains digits
    # - contains letters
    # - etc.
    # details hidden inside
    ...

In the rest of your code, you just need to know:

This separation lets you:

Abstraction makes big programs manageable by hiding unnecessary detail until you specifically need it.

Making testing and debugging easier

Functions make it easier to test small pieces of your program in isolation.

Instead of running the whole program every time, you can:

Example: if your receipt total is wrong, you can directly test:

print(calculate_total([10, 20, 30]))
print(calculate_total([]))
print(calculate_total([5]))

Once you trust that calculate_total works correctly, you can look elsewhere if the final result is still wrong.

This style—testing individual functions separately—is known as unit testing (each function is a “unit” of your program). Even informal testing like this becomes much easier when your code is organized into functions.

Enabling teamwork

On larger projects, multiple people may work on the same program. Functions help teams divide the work.

For example, in a game project:

As long as everyone agrees on:

they can work mostly independently. Later, these functions are combined into a complete program.

Even when you’re working alone, thinking in this way prepares you for larger collaborative projects.

Thinking in terms of “actions”

Functions encourage you to think of your program as a collection of actions:

This “verb-based” view (each function is like a verb) can make it easier to design your program:

  1. Describe what your program needs to do in plain language.
  2. Underline the actions.
  3. Turn those actions into function ideas.

For example, a “to-do list” app might involve actions like:

These can become:

Designing your program around clear actions often leads to cleaner, more organized code.

How this connects to the rest of the chapter

In the rest of this chapter, you’ll see how to work with functions in Python, including:

Keep the main ideas from this section in mind while you learn the details:

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!