A function or procedure is a named, reusable block of code that performs a specific task. Writing one means you can run that block of code as many times as you like, from anywhere in your program, just by using its name — making programs shorter, easier to read, and far simpler to fix when something goes wrong.
Why do KS3 computing teachers emphasise functions?
The DfE national curriculum for KS3 computing requires students to "use two or more programming languages, at least one of which is textual" and to "understand several key algorithms." Functions are the principal tool for implementing the decomposition part of computational thinking: breaking a large program into smaller, manageable pieces. Every professional program is built from functions. Learning to use them at KS3 sets students up directly for GCSE Computer Science, where subroutines appear in every examination specification.
What is the difference between a function and a procedure?
Both are blocks of named, reusable code — collectively called subroutines. The distinction is:
| Feature | Procedure | Function |
|---|---|---|
| Returns a value? | No — performs an action | Yes — calculates and returns a result |
| Called with | A statement: print_greeting() |
Usually an expression: result = add(3, 4) |
| Python keyword | def (same syntax; no return value) |
def with a return statement |
| Real-world analogy | Pressing a button that starts an engine | Putting coins in a vending machine and getting a snack back |
In Python, both are defined with def. The presence or absence of a return statement is what distinguishes them behaviourally.
How do you write and call a procedure in Python?
def greet_student():
print("Good morning!")
print("Are you ready to learn?")
# Calling the procedure:
greet_student()
greet_student()
The procedure greet_student contains two lines of code. Calling it twice runs those two lines twice — without writing them twice. If you later want to change the greeting, you change it in one place only.
How do you write and call a function with parameters and a return value?
def add_numbers(a, b):
total = a + b
return total
# Calling the function:
result = add_numbers(5, 3)
print(result) # Outputs: 8
print(add_numbers(10, 20)) # Outputs: 30
Parameters (a and b) are the input values the function receives. Arguments are the actual values passed when calling (5 and 3). The return statement sends the calculated value back to wherever the function was called.
Worked example: a program using multiple functions
Here is a short grade-feedback program built from functions:
def get_grade(score):
if score >= 70:
return "Distinction"
elif score >= 50:
return "Merit"
else:
return "Pass"
def display_result(name, score):
grade = get_grade(score)
print(name + " scored " + str(score) + " — " + grade)
# Main program
display_result("Amara", 82)
display_result("Ben", 55)
display_result("Cleo", 43)
Trace table:
| Call | score | Returned grade | Output |
|---|---|---|---|
| display_result("Amara", 82) | 82 | "Distinction" | Amara scored 82 — Distinction |
| display_result("Ben", 55) | 55 | "Merit" | Ben scored 55 — Merit |
| display_result("Cleo", 43) | 43 | "Pass" | Cleo scored 43 — Pass |
Notice how display_result calls get_grade internally. Functions can call other functions — this is how large programs are built from small, testable pieces.
What is variable scope?
Scope determines where a variable can be seen and used in a program.
- A local variable is declared inside a function. It only exists while that function is running. Once the function finishes, the local variable is deleted.
- A global variable is declared outside all functions. It can be seen from anywhere in the program.
total = 0 # global variable
def add_to_total(number):
result = number + 1 # local variable — only exists inside this function
return result
print(result) # ERROR: 'result' is not defined here — it was local
Good programming style prefers local variables and passing data through parameters and return values. Relying heavily on global variables makes programs harder to debug.
Why are functions better than copying and pasting code?
Imagine writing a 20-line block of code, then copying it to five different places in your program. If you find a bug in that block, you must find and fix it in all five places — and risk missing one. With a function:
- Write the code once.
- Fix bugs in one place.
- Test the function once and trust it everywhere it is used.
- Give the function a meaningful name so the code reads like English.
This principle is called DRY — Don't Repeat Yourself — and it is one of the most important rules in software engineering.
Frequently asked questions
What is a parameter?
A parameter is a variable listed in a function's definition that acts as a placeholder for a value to be supplied when the function is called. For example, in def multiply(x, y):, x and y are parameters. The actual values passed when calling, such as multiply(3, 7), are called arguments. Inside the function, x takes the value 3 and y takes the value 7.
Can a function return more than one value?
In Python, a function can return multiple values by separating them with commas: return x, y. Python packs them into a tuple. The calling code can unpack them: a, b = my_function(). This is useful when a function needs to send back related pieces of information — for example, both the quotient and the remainder of a division.
What is a built-in function?
A built-in function is one provided by the programming language itself, ready to use without being defined by the programmer. Python's print(), len(), int(), str(), input(), and range() are all built-in functions. They work in exactly the same way as functions you write yourself — you call them by name and, for most, pass arguments inside the brackets. Using built-in functions saves time and makes use of code that has been tested by the language developers.
What does "calling" a function mean?
Calling a function means executing it — running the code inside it. You call a function by writing its name followed by parentheses, with any required arguments inside: get_grade(75). At the moment of the call, the program jumps to the function definition, runs the code, then returns to the line immediately after the call. This jumping-and-returning is managed automatically by Python's call stack.
For Socratic KS3 and GCSE computing tutoring — from functions to full programs — visit aitutors.me.