Functions: Reusable Blocks of Code
A middle-school coding lesson on functions: learn how to define and call functions, pass arguments, return values, and why functions keep code clean, with Python examples and a quiz.
Key takeaways
- A function is a named, reusable block of code that does one job
- You define a function once, then call it as many times as you like
- Arguments pass information into a function so it can work on different inputs
- A return value sends a result back out of the function
- Functions keep code short, organized, and easier to fix
The problem functions solve
Imagine you need to greet three people in your program:
print("Hello, Sam!")
print("Have a great day, Sam!")
print("Hello, Maya!")
print("Have a great day, Maya!")
print("Hello, Ali!")
print("Have a great day, Ali!")
That's a lot of repeated typing, and if you ever want to change the greeting, you have to fix it in many places. There's a better way: a function.
A function is a named, reusable block of code that does one job. You define it once, then call it whenever you need it.
Defining a function
In Python, you create a function with the def keyword:
def greet():
print("Hello!")
print("Have a great day!")
This defines a function called greet. The indented lines are the function's body — the code it runs. Defining a function does not run it yet. It just teaches Python what greet means.
Calling a function
To actually run the function, you call it by writing its name with parentheses:
greet()
greet()
This prints the greeting twice:
Hello!
Have a great day!
Hello!
Have a great day!
Write the steps once, run them as many times as you like. That's the power of functions.
Passing in information with arguments
Our greeting always says the same thing. To make it flexible, we pass in information using a parameter:
def greet(name):
print("Hello, " + name + "!")
print("Have a great day, " + name + "!")
Here name is a parameter — a placeholder for a value. When we call the function, we give it an argument, the real value:
greet("Sam")
greet("Maya")
greet("Ali")
Now the same three-line function handles all three people:
Hello, Sam!
Have a great day, Sam!
Hello, Maya!
...
Compare that to the repeated code at the top. Much shorter, and changing the greeting now means editing just one place.
Returning a value
Some functions don't just do something — they calculate a result and hand it back. For that we use return:
def double(n):
return n * 2
The return keyword sends a value back out of the function. We can store it in a variable or print it:
result = double(5)
print(result) # prints 10
print(double(7)) # prints 14
When Python reaches return, it stops the function and sends the value back to wherever the function was called. A function can use that result in math, in an if statement, or anywhere a value fits.
Functions and loops together
Functions and loops are a powerful team. A loop can call a function many times:
def square(n):
return n * n
for i in range(1, 6):
print(i, "squared is", square(i))
This prints:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
The loop handles the repetition; the function handles the calculation. Each part does one clear job.
Why functions matter
Functions are a cornerstone of good programming because they let you:
- Reuse code instead of copying it (programmers call this "Don't Repeat Yourself").
- Organize a big program into small, named pieces, each doing one job.
- Fix bugs faster — when a job is wrong, you correct it in one place. Pair this with careful debugging.
- Hide detail — once
squareworks, you can use it without thinking about how it works inside.
This idea scales all the way up to professional software and artificial intelligence, where huge programs are built from thousands of small functions working together. Master functions, and you can build programs of almost any size.
Quick quiz
Test yourself and earn XP
What is a function?
A function is a named block of code you can define once and reuse whenever you need it.
What does this print? ``` def double(n): return n * 2 print(double(5)) ```
double(5) returns 5 times 2, which is 10, so it prints 10.
What is an argument?
An argument is a value you pass into a function so it can work on that input.
What does the 'return' keyword do?
return sends a result back to wherever the function was called.
Why are functions useful?
Functions let you write a block once and reuse it, keeping code short and easy to maintain.
FAQ
A parameter is the name listed when you define the function, like n in def double(n). An argument is the actual value you pass in when you call it, like the 5 in double(5).
In Python, a function with no return automatically returns a special value called None. It can still do useful work, like printing, even without returning a value.
Keep exploring
More in Coding