DEV Community

Feddy Mwanjumwa
Feddy Mwanjumwa

Posted on

Python Functions: How I Learned to Reuse My Code

When I started writing more Python, I noticed I was repeating the same pieces of code.

At first, I didn't think much of it. But after a while, I realised there had to be a better way.

That's when functions started making sense to me.

A function lets me write a piece of code once and use it whenever I need it.

Defining a function
I create a function using def.

For example:

def greet():

print("Hello, welcome!")

Nothing happens yet because I have only defined the function.

To actually run it, I call it:

greet()

This was one of the first things I understood about functions: defining a function and calling a function are two different things.

Parameters and arguments

Functions become more useful when I can give them information.

For example:

def greet(name):

print(f"Hello, {name}!")

Now I can pass a name:

greet("Feddy")

The result is:

Hello, Feddy!

The name inside the function is the parameter, while "Feddy" is the argument I pass to it.

That distinction took me a little practice to remember.

Multiple parameters

I can also pass more than one value.

For example, I can calculate the total cost of a product:

def calculate_total(price, quantity):

total = price * quantity

print(total)

Then:

calculate_total(500, 3)

The result is:

1500

This is much better than writing the same calculation every time I need it.

Return values

This was another important part for me.

A function can return a value instead of just printing it.

For example:

def calculate_total(price, quantity):

return price * quantity

Now I can store the result:

total = calculate_total(500, 3)

print(total)

The result is:

1500

The difference between print() and return became much clearer to me here.

print() displays something.

return sends a value back so I can use it somewhere else.

Scope

Scope is basically about where a variable can be accessed.

For example:

name = "Feddy"

def greet():

name = "Amina"

print(name)

greet()

print(name)

Inside the function, the result is:

Amina

Outside the function, the result is:

Feddy

The name created inside the function is local to that function.

Understanding this helped me avoid accidentally changing or depending on variables in the wrong place.

A practical example

One simple example I used was calculating a net salary.

Suppose someone's salary is KSh 85,000 and their deductions are KSh 4,500.

I can create:

def calculate_net_salary(salary, deductions):

return salary - deductions

Then:

net_salary = calculate_net_salary(85000, 4500)

print(f"Net salary: KSh {net_salary}")

The result is:

Net salary: KSh 80500

Now I can use the same function for different employees without rewriting the calculation.

Why functions became useful to me

Before functions, I was thinking:

"I need to write this calculation again."

After learning functions, I started thinking:

"I've already written this once. I'll just call the function."

That small change made my code much cleaner.

What I understood

The way I now think about functions is pretty simple:

def → create the function

Parameters → information the function expects

Arguments → values I actually pass

return → send a result back

Scope → where variables can be accessed

Once these pieces made sense, functions stopped feeling complicated.

Final thoughts

Functions were one of the first things that made me feel like I was actually organising my code rather than just writing lines of Python.

They helped me avoid repetition, break bigger programs into smaller parts, and reuse code whenever I needed it.

For me, the biggest lesson was simple: write it once, then reuse it.

Top comments (0)