What Is a Function (Subroutine) in Programming? KS3 Explained

A function — also called a subroutine in some specifications — is a named, reusable block of code that performs a specific task. Instead of writing the same instructions multiple times, you define a function once and call it whenever you need it. Functions make programs shorter, easier to read, and far simpler to debug.

What is a function and why use one?

Imagine a set of instructions for making a cup of tea. You could write those instructions out in full every single time someone wants tea, or you could write them once under the heading "make tea" and simply say "make tea" whenever you want that to happen. A function in programming works exactly the same way.

Functions serve three core purposes:

Reusability — write the code once, use it as many times as needed without copying and pasting.

Decomposition — break a large, complex program into smaller, manageable named chunks. This is a key element of computational thinking.

Maintainability — if you need to change how a task is performed, you change it in one place (the function definition), and every call to that function automatically reflects the change.

The DfE computing programme of study requires KS3 students to use abstraction and decomposition, and to use at least one textual programming language to solve computational problems (gov.uk/government/publications/national-curriculum-in-england-computing-programmes-of-study). Functions are the primary programming mechanism for both.

How do you define a function in Python?

In Python, you define a function using the def keyword, followed by the function name, parentheses, and a colon. The body of the function is indented.

def greet():
print("Hello! Welcome to the quiz.")
print("Good luck!")

To call (run) the function, simply write its name followed by parentheses:

greet() # Output: Hello! Welcome to the quiz.
# Good luck!
greet() # The same two lines are printed again.

Notice that the two lines inside greet only need to be written once. Every time the program calls greet(), both lines execute.

What are parameters and arguments?

A parameter is a variable listed inside the parentheses in the function definition. It acts as a placeholder for the value that will be passed in when the function is called. An argument is the actual value passed to the function at the point of calling.

def greet(name): # 'name' is the parameter
print("Hello, " + name + "! Welcome to the quiz.")

greet("Aisha") # "Aisha" is the argument — name = "Aisha"
greet("Jordan") # "Jordan" is the argument — name = "Jordan"

Output:

Hello, Aisha! Welcome to the quiz.
Hello, Jordan! Welcome to the quiz.

A function can have multiple parameters, separated by commas:

def add(a, b):
print(a + b)

add(3, 7) # Output: 10
add(100, 25) # Output: 125

What is a return value?

Some functions do more than print something — they calculate a result and send it back to wherever the function was called from. The return statement is used for this.

def area_of_rectangle(length, width):
area = length * width
return area

result = area_of_rectangle(5, 3)
print("The area is", result, "cm²") # Output: The area is 15 cm²

The return statement ends the function immediately and sends the specified value back. That value can be stored in a variable (result = area_of_rectangle(5, 3)) or used directly (print(area_of_rectangle(5, 3))).

A function that returns a value is called a function (or sometimes a fruitful function). A function that performs an action but does not return a value is often called a procedure in some specifications (particularly in pseudocode and OCR's exam notation).

Term | Returns a value? | Example
Function | Yes — uses return | area_of_rectangle(5, 3) returns 15
Procedure | No — performs an action only | greet("Aisha") prints but returns nothing

What is the difference between built-in and user-defined functions?

Built-in functions come with Python and are available to use immediately: print(), input(), len(), int(), str(), range(), and many more. You do not need to define them — they are part of the language.

User-defined functions are ones you create yourself using def. Every function you write in a program is a user-defined function.

# Built-in functions
name = input("Enter your name: ")
length = len(name)
print("Your name has", length, "characters.")

# User-defined function
def is_long_name(name):
return len(name) > 8

if is_long_name("Bartholomew"):
print("That's a long name!")

A worked example: grade calculator

The following program uses three functions to calculate and display a student's grade:

def calculate_percentage(marks, total):
return (marks / total) * 100

def get_grade(percentage):
if percentage >= 70:
return "Distinction"
elif percentage >= 55:
return "Merit"
elif percentage >= 40:
return "Pass"
else:
return "Fail"

def display_result(name, marks, total):
percentage = calculate_percentage(marks, total)
grade = get_grade(percentage)
print(name + " scored " + str(round(percentage, 1)) + "% — Grade: " + grade)

display_result("Priya", 68, 80)
display_result("Marcus", 45, 80)
display_result("Leila", 30, 80)

Output:

Priya scored 85.0% — Grade: Distinction
Marcus scored 56.2% — Grade: Merit
Leila scored 37.5% — Grade: Fail

Each function has a single, clearly named job. calculate_percentage does the maths. get_grade applies the grading rules. display_result brings them together. Notice how display_result calls calculate_percentage and get_grade — functions can call other functions.

Frequently asked questions

What is the difference between a function and a subroutine at KS3?

The terms are largely interchangeable at KS3. "Subroutine" is the general umbrella term for any named, reusable block of code. Some specifications (particularly OCR) use "function" for subroutines that return a value and "procedure" for those that do not. In Python, all subroutines are defined with def, regardless of whether they return a value.

What is a parameter and what is an argument?

A parameter is the variable name listed in the function definition (e.g. def greet(name) — name is the parameter). An argument is the actual value passed in when the function is called (e.g. greet("Aisha") — "Aisha" is the argument). Parameters are placeholders; arguments are the real values that fill those placeholders at runtime.

Why should I use functions instead of just writing the code directly?

Functions prevent repetition (the DRY principle — Don't Repeat Yourself), make programs easier to read and test, and allow you to fix a bug in one place rather than hunting through every repeated copy of the same code. In GCSE exam questions, breaking a problem into subroutines also demonstrates computational thinking, which attracts marks.

What happens if a function does not have a return statement?

A Python function without a return statement performs its actions (prints, updates variables, etc.) and then returns the special value None. This is fine for procedures that just need to do something, but if you try to store the result of such a function in a variable and use it as a number or string, you will get unexpected behaviour. Always check whether you need a return value before deciding whether to include return.

For Socratic computing tutoring at KS3 and GCSE — see aitutors.me.