AQA 7517, section 4.4.1.5
Procedural abstraction represents a computational method. It takes a specific computation and removes the actual values used in it, leaving a general pattern that can be applied to any values. The result of that abstraction is a procedure.
From specific to general
Suppose you need the area of several squares:
area1 = 5 * 5
area2 = 8 * 8
area3 = 12 * 12
The pattern is the same each time: take a number and multiply it by itself. Only the actual value changes. Procedural abstraction replaces the particular values with a parameter:
def square(n):
return n * n
area1 = square(5)
area2 = square(8)
area3 = square(12)
square is a computational method: "multiply a number by itself". It is no longer tied to 5, 8 or 12. This is a procedural abstraction. The result of abstracting away the actual values is a procedure.
How the steps look
| Stage | What you have |
|---|---|
| Specific computations | 5 * 5, 8 * 8, 12 * 12 |
| Spot the common pattern | "a number times itself" |
| Replace the values with a parameter | n * n |
| Give the pattern a name | square(n) |
Once named, anyone can use square(7) without thinking about how it works. They only need to know what it does.
Benefits
- Avoids repetition: the method is written once and reused.
- Easier to understand: a call such as
square(n)is quicker to read than the details. - Easier to maintain: if the method needs correcting, it's fixed in one place.
- Builds up layers: procedures can be used to build bigger procedures (see composition, 4.4.1.10).
A longer example
def show_total(price, quantity):
total = price * quantity
print("Total:", total)
show_total(2.50, 4)
show_total(12.00, 3)
The particular price and quantity are abstracted away. What remains is the method for working out and displaying a total.
Worked example
Task: these lines each calculate a percentage. Write a procedural abstraction.
vat1 = 80 * 20 / 100
vat2 = 150 * 20 / 100
tip = 45 * 10 / 100
The common pattern is "an amount multiplied by a rate, divided by 100". The values 80, 150, 45 (amounts) and 20, 10 (rates) are replaced with parameters:
def percent_of(amount, rate):
return amount * rate / 100
vat1 = percent_of(80, 20) # 16.0
vat2 = percent_of(150, 20) # 30.0
tip = percent_of(45, 10) # 4.5
percent_of is the procedure: a computational method that works for any amount and any rate.
Exam tip: the key phrase is that procedural abstraction represents a computational method, by abstracting away the actual values, so what is left is a pattern that can be reused. Back it up with a short example, such as the square procedure.
Check yourself
What is procedural abstraction?
Abstracting away the actual values used in a particular computation to leave a general computational method, which is a procedure.
What replaces the actual values when a procedure is created?
Parameters.
Give two benefits of procedural abstraction
Any two of: avoids repeating code; makes code easier to read; changes only need to be made in one place; procedures can be combined to build bigger ones.