Good code does not repeat itself. Ever.
Every time you write the same lines twice, something is wrong. Not with you – with the approach. Professional developers solve this with one simple concept that changes how you think about code entirely.
It is called a function. In this guide, you will learn exactly what functions are, why they matter, and how to write your own – step by step, with examples that make the logic feel natural from the start.
What is a function
in coding?
The idea that lets you write code once and use it anywhere — the backbone of every real program ever written.
A function is a reusable block of code
A function is a named block of code that does one specific job. You write it once, give it a name, and call it whenever you need it — without writing the same code again.
The first time I used a function, I realized I had been copy-pasting the same 5 lines everywhere. One function replaced all of them. That was the moment it clicked for me.
Think of a vending machine
A vending machine works exactly like a function. You put something in (your money + selection), it does something with it (processes your order), and gives something back (your snack). You don’t care how it works inside — you just use it.
That’s exactly how a function works in code — input goes in, something happens inside, output comes out.
Without a function vs with a function
Say you need to greet three users. See how a function cuts the repetition completely:
print("Hello, Ali!")
print("Hello, Sara!")
print("Hello, Hamza!")
100 users = 100 lines.
def greet(name): print(f"Hello, {name}!") greet("Ali") greet("Sara") greet("Hamza")
100 users = still 2 lines.
Why do functions
matter in programming?
Functions aren’t just a coding concept — they’re the reason real programs don’t fall apart. Here’s exactly what they do for you.
They stop you from repeating code
Without functions, you copy-paste the same code in five different places. Then if something needs to change — you have to fix it in five places. Miss one? Your program breaks.
With a function, you fix it once and every call updates automatically.
They make your code easier to read
A function has a name that describes what it does. Instead of reading 10 lines of logic, you just see calculateTotal() — and instantly know what’s happening.
- ✓ Anyone can read your code — even months later
- ✓ Short function names replace long blocks of logic
- ✓ Teams work faster when code is organized
They help you fix bugs faster
When a bug shows up, functions tell you exactly where to look. Each function does one job — so if something’s broken, you check that one function, fix it, and you’re done.
- ✓ Bug in greetUser()? Check that function only
- ✓ No hunting through hundreds of lines
- ✓ Fix once — works everywhere it’s called
How does a function
work? (step-by-step)
Every function follows the same three steps — no matter what language you use. Learn these and functions will never feel confusing again.
Step 1 — Define the function
Defining a function means writing it for the first time and giving it a name. At this point, the code inside doesn’t run yet — you’re just telling the program “this function exists and here’s what it does.”
Think of it like writing a recipe. You write it down, but the cooking hasn’t started.
def greet(name): print("Hello,", name) # defined — but not running yet
Step 2 — Call the function
Calling a function means actually running it. You write the function name followed by parentheses — and that’s it. The code inside now executes.
This is the part beginners often miss — defining a function doesn’t run it. You have to call it separately.
def greet(name): print("Hello,", name) greet("Ali") # ← calling the function greet("Sara") # ← calling again
Step 3 — The function runs and returns a result
Once called, the function runs its code top to bottom. If it has a return statement, it sends a result back to wherever it was called from — like a machine spitting out your output.
Without return, the function just runs and stops. With return, it hands something back you can use.
def add(a, b): return a + b # ← sends result back result = add(3, 5) print(result) # OUTPUT → 8
Parts of a function —
explained simply
Every function has four parts. Know each one and writing functions becomes second nature.
Function name
The name is how you identify and call the function later. Pick a name that describes exactly what it does — like calculateTotal or greetUser.
Good names make your code readable without needing extra explanation.
Parameters (inputs)
Parameters are the inputs your function needs to do its job. You put them inside the parentheses. When you call the function, you pass in the actual values — those are called arguments.
Function body
The body is the actual code that runs when the function is called. It’s indented inside the function and can be as short as one line or as long as needed.
Everything the function does lives here.
Return value (output)
The return statement sends a result back to wherever the function was called. Not every function needs one — but when you need a result, return is how you get it out.
Best practices for
writing functions
Writing a function is easy. Writing a good function takes a little more thought. These simple habits make your code cleaner from day one.
Each function should do exactly one thing. If your function is greeting users AND saving to a database, split it into two. Small, focused functions are easier to read, test, and fix.
def greet() ✓ def save() ✓A good function name tells you exactly what it does without reading the code inside. calculate_tax() is clear. do_thing() means nothing.
If a function is getting long, it’s probably doing too much. Aim for 10–20 lines max. Long functions are hard to read and harder to debug. Break them into smaller ones.
10–20 lines = sweet spotA function should not secretly change things outside itself. If it modifies a global variable without warning, it creates bugs that are nearly impossible to track down.
return result — don’t mutate globalsOne of the biggest advantages of functions is that you can test each one on its own. Call it with different inputs, check the output, and fix it before connecting it to the rest of your code.
I always test my functions alone first. It saves hours of debugging later.
def add(a, b): return a + b # test it alone first print(add(2, 3)) # 5 ✓ print(add(-1, 1)) # 0 ✓ print(add(0, 0)) # 0 ✓
FAQs about
functions in coding
Quick answers to the questions beginners ask most about functions.
A loop repeats the same code multiple times in a row. A function is a reusable block you can call from anywhere in your program — not just once in sequence.
Think of it this way: a loop runs code again and again right where it sits. A function sits separately and waits to be called — as many times as you need, from anywhere.
Most beginner languages group functions into four main types:
Yes — and this is how real programs are built. One function can call another function inside it. Each function does its small job, and together they build something powerful.
The function runs its code and returns None automatically. None means “nothing” — no value came back. This is fine if you just want to print or run an action, but it breaks things if you expected a result.
Always add return if your function needs to send something back.