What Is a Function in Coding? (Write It Once, Use It Everywhere)
A function is one of those ideas that sounds obvious once you get it — and then you start seeing it everywhere.
Here's the Short version (literally):
Now let's go deeper.
The Problem You've Probably Already Hit
You're writing a program. You need to do the same thing in three different places.
Maybe it's showing a score. Maybe it's checking if a number is valid. Maybe it's printing a welcome message with someone's name.
So you write the code. Then you write it again. Then again.
And then you realise you need to change something — and now you've got to change it in three places. Miss one, and your program breaks in a weird way that takes ages to find.
That's the problem functions solve.
What Is a Function? (The Classroom Version)
Think of a function like a machine at school.
The teacher builds a machine that sharpens pencils. Every time a student needs a sharp pencil, they put their blunt one in the slot. The machine does its thing. A sharp pencil comes out.
The teacher didn't have to explain how pencil-sharpening works every single time. She built the machine once. Now anyone can use it.
That's a function. You define it once — you give it a name, tell it what to do — and then you call it whenever you need that thing done.
The Python Version
Here's the simplest function in Python:
def say_hello():
print("Hello, world!")
Two things happening here:
deftells Python you're defining a functionsay_hellois the name you're giving it
Now you can call it:
say_hello()
say_hello()
say_hello()
That prints "Hello, world!" three times. You only wrote the printing code once.
Making It More Useful — Add an Input
Right now say_hello() always says the same thing. But what if you want to say hello to different people?
You give the function an input (called a parameter):
def say_hello(name):
print("Hello, " + name + "!")
Now you can call it with different values:
say_hello("Priya")
say_hello("Callum")
say_hello("Ms Davies")
Output:
Hello, Priya!
Hello, Callum!
Hello, Ms Davies!
One function. Three different results. The machine works the same way — but the input changes what comes out.
What About Getting Something Back?
Sometimes you don't just want to print something. You want the function to give you a value you can use elsewhere.
That's what return does:
def add_two_numbers(a, b):
result = a + b
return result
Now you can store what comes back:
total = add_two_numbers(5, 3)
print(total) # prints 8
The function ran, did the maths, and handed the answer back. You can now do whatever you like with total.
Where Functions Show Up in Real Builds
Once you start recognising functions, you see them in everything.
In the Farmer Series — when the drone needs to move to a specific square on the grid, that's a function call. move_to(x, y). Define it once, use it every time the drone moves. Without functions, you'd be rewriting the movement logic 9 times (once per grid square).
In the chatbot build — every time a user message comes in, there's a function called handle_message(user_input). It runs the same logic every time. The input changes, the logic stays the same.
In Scratch — even Scratch has functions. They're called Custom Blocks. If you've ever made a "jump" block that you reuse across a game, you've already written a function without knowing it.
The Three Things to Remember
def— you're defining a function (building the machine)- Parameters — inputs you pass in (what goes in the slot)
return— the value you get back (what comes out the other side)
That's it. That's the whole concept.
What Comes Next
Functions get a lot more powerful when you:
- Combine them — functions that call other functions
- Use them with loops — call the same function across a list of items
- Import them — Python has thousands of pre-built functions in libraries (you don't have to write everything yourself)
We'll get to all of that. But if you understand define once, call anywhere — you've got the heart of it.
Keep building.