AQA 7517, section 4.4.1.6
In functional abstraction, the particular computation method is hidden. All that matters is what goes in and what comes out. How the result is calculated doesn't matter to the user.
From procedure to function
Remember that procedural abstraction (4.4.1.5) gives you a procedure: a computational method with the actual values replaced by parameters. You still know how it works, because its method is visible.
Functional abstraction goes one step further. It disregards the particular computation method altogether. A function is just a mapping from inputs to outputs.
| Procedural abstraction | Functional abstraction | |
|---|---|---|
| Hides | The actual values | The method of computation as well |
| Result | A procedure | A function |
| Focus | "Follow these steps for any values" | "Given this input, you get this output" |
So the result of a procedural abstraction is a procedure, not a function. To get a function you need yet another abstraction, which ignores the method used.
Example: sorting
Think about the idea "sort a list". The user needs to know only that:
- input: a list of numbers in any order;
- output: the same numbers in ascending order.
How it is done doesn't matter. It could use bubble sort, merge sort or something else.
data = [5, 2, 9, 1]
result = sorted(data) # a function: input list, output sorted list
print(result) # [1, 2, 5, 9]
The caller has no idea (and no need to know) which algorithm sorted uses. If the maker replaces it with a faster method, the code still works, because the function, meaning the mapping from input to output, is unchanged.
Another way to see it: two different methods that give the same outputs for the same inputs are the same function.
def square_a(n):
return n * n
def square_b(n):
total = 0
for _ in range(n):
total = total + n
return total
print(square_a(6), square_b(6)) # 36 36
These use two different methods, but give identical results for whole numbers, so they represent the same function: .
Why is this useful?
- You can think at a higher level and combine functions without worrying about their insides.
- The method can be replaced by a better one without changing anything that uses it.
- It supports information hiding (4.4.1.4), because the method is concealed.
Worked example
Task: explain the difference between the procedural and functional abstraction of "find the average of a list".
- Procedural abstraction: replace specific numbers with a parameter, giving a procedure
average(numbers): add them up and divide by how many there are. The method is a visible part of the abstraction. - Functional abstraction: disregard how the average is found. The only thing that matters is the mapping: a list of numbers goes in, and its mean comes out. The calculation could use a loop,
sum(), or any other method, and the result for each input would be the same.
Exam tip: state the key point precisely. In functional abstraction, the particular computation method is hidden. Contrast it with procedural abstraction, which removes the values but still gives you a method (a procedure).
Check yourself
What is hidden in functional abstraction?
The particular computation method. Only the mapping from inputs to outputs matters.
What is the result of procedural abstraction, and what extra step is needed to get a function?
Procedural abstraction results in a procedure. To get a function you need a further abstraction that disregards the particular computation method.
Why can bubble sort and merge sort be seen as the same function?
For any input list, both give the same output, the list in ascending order. They differ only in the method used, which functional abstraction ignores.