The big idea: A function is a named block of code that does one job, can be given different inputs, and hands back a result.
Why it pays: Write it once, call it anywhere — and fix it in one place when it turns out to be wrong.
def average(values): # values is a PARAMETER
if len(values) == 0:
return 0 # the empty case, handled once
return sum(values) / len(values)
print(average(maths_marks)) # maths_marks is an ARGUMENT
print(average(science_marks)) # same code, different datastatic double average(int[] values) { // values is a PARAMETER
if (values.length == 0) {
return 0; // the empty case, handled once
}
int total = 0;
for (int v : values) { total = total + v; }
return (double) total / values.length;
}
System.out.println(average(mathsMarks)); // mathsMarks is an ARGUMENT
System.out.println(average(scienceMarks)); // same code, different dataParameter and argument: The parameter is the name in the definition — the placeholder.
The argument is the actual value passed in when it is called. Definition has parameters; calls supply arguments.
- Method (function) signature
- Its name and its parameter list: how many parameters, in what order, of what types. The return type is not part of it.
- Parameter
- The variable named in the definition that receives a value: values in average(values).
- Argument
- The actual value passed in the call: marks in average(marks).
- Sub-program as abstraction
- You use it by its name and inputs only; how it works stays hidden inside it.
Watch a call happen: the program jumps in, works with its own variables, and comes back with one value.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
Reusable
- One definition serves every place that needs the job done
- No copied blocks that then drift apart
- A fix applies everywhere at once
Maintainable
- Each function is small enough to understand completely
- A fault is traced to one function, not scanned for across a program
- It can be changed inside without touching anything that calls it
Testable and shareable
- A function can be tested on its own, with values chosen to break it
- Work can be split: different people write different functions
- Both only work because the boundary is clearly defined
The copy-paste argument: Copy a calculation into four places and you have four places to fix when it is wrong — and you will miss one.
Call a function in four places and there is one.
That is the strongest single argument for modularity, and the one to write down.
Modularisation is bigger than functions: It also means grouping related functions into modules or files — all the file handling in one place, all the calculations in another.
Same idea, one level up.
Never wonder what to study next
Get a personalized daily plan based on your exam date, progress, and weak areas. We'll tell you exactly what to review each day.
tax_rate = 0.2 # global — a genuine setting
def with_tax(price):
amount = price * tax_rate # local: amount. Reads the global rate
return price + amount # return hands the result back
print(with_tax(50)) # 60.0
print(amount) # error — amount only existed insidestatic double taxRate = 0.2; // a field — a genuine setting
static double withTax(double price) {
double amount = price * taxRate; // local: amount. Reads the shared rate
return price + amount; // return hands the result back
}
System.out.println(withTax(50)); // 60.0
System.out.println(amount); // error — amount only existed insideIn through parameters
A function should be told what it needs, rather than reaching out for it.
That way it works anywhere, can be tested on its own, and cannot be broken by something changing elsewhere.
Out through return
return sends a value back and ends the function immediately.
A function that changes a global instead is harder to test and harder to reuse — because its effect is invisible from the call.
Local stays inside
Variables created in a function disappear when it ends.
That is a feature: two functions can safely use i or total with no risk of interfering.
Globals, sparingly
A genuine setting — a tax rate, a file path — is reasonable.
Using them to pass working values between functions is what makes a program hard to follow, because any function might be the one that changed it.
A function with no return gives nothing back: If it only prints, the value cannot be used for anything else.
return the result and print it at the call if needed — a function that returns can be tested; one that only prints cannot.
How this is tested — you must define with parameters, return a value, and say why it is better than repeating the code. It comes up two ways:
Paper 2 — working with code
- Construct a function for a stated job, 3-5 marks
- Explain the benefits of modularisation
- Identify a scope error in given code
Paper 2 — the algorithmic-thinking question
- Explain why a program should be split up
- Reason about scope with no code shown
The classic trap: Writing a function that uses a global instead of a parameter, and prints instead of returning. It may work in the one place it was written, and it cannot be reused or tested anywhere else.
A program calculates a percentage in four different places. Write a function for it and explain why this is better.
Model answer plan
See the answer broken into clear points, with guidance on how each point earns marks.