Procedural and Data Abstraction
Use a Procedure Without Unpacking Its Code
Procedural abstraction lets you use a named operation through its stated behavior without following its internal instructions. You meet it whenever you call a procedure to calculate a charge, convert a measurement, or send a message. Your task as the caller is to supply appropriate inputs and use the promised result. The procedure takes responsibility for carrying out the operation.
Separate the task from its implementation
The caller is the code that invokes a procedure. It supplies arguments, such as the numbers in copy_cost_cents(8, 3). The procedure then executes its internal instructions. Depending on its specification, it may return a value, produce visible changes, or do both.
For a procedure that deterministically returns a value without other visible changes, the central relationship is:
Here, the inputs determine the returned result. This relationship describes what the operation does; it does not prescribe whether its implementation uses multiplication, repeated addition, or another suitable method.
A procedure can be useful even if it is called only once. Giving an operation a meaningful name can let you understand the surrounding computation at the level of tasks rather than individual instructions.
Read what a caller can rely on
A procedure specification states the behavior that callers may rely on. Useful information includes the accepted inputs, the meaning of the returned result, and any side effects. A procedure signature identifies its name and inputs, and often its return type, but those details alone rarely explain the whole operation.
For example, copy_cost_cents(pages, copies) accepts a nonnegative whole number of pages and a positive whole number of copies. It returns an integer charge in cents at six cents per page per copy. It does not print anything or change shared information.
Example: Calling copy_cost_cents(8, 3) returns 6×8×3=144 cents. The caller can add that returned number to another charge. It does not need to know how the procedure calculated 144.
Returning a value and displaying a value are different behaviors. A procedure that prints 144 but supplies no usable return value does not satisfy this specification. Likewise, an input outside the stated range has no promised result unless the specification describes one.
Change the steps while preserving the behavior
Procedural abstraction allows an implementation to change while existing callers continue to work. The replacement needs to preserve the specified behavior for every accepted input, including boundary cases such as zero pages. Agreement on one example is useful evidence, but it does not establish agreement across the whole input range.
Proposed change | Compatible with the stated specification? | Reason |
|---|---|---|
Replace repeated addition with multiplication | Yes, if all accepted inputs give the same results | The calculation method is internal |
Return dollars instead of cents | No | The meaning of the returned number changes |
Print a receipt as well as returning the charge | No | Printing adds a visible effect that was excluded |
Rename a variable used only inside the procedure | Yes, if its behavior stays unchanged | Callers do not use that local name |
This separation lets you reason about a larger calculation using smaller, named operations. It does not make every implementation equally fast, and it does not remove the need to check that each implementation actually meets its specification.
What to remember
- Use a procedure through its accepted inputs, promised results, and stated visible effects.
- Keep returned values distinct from displayed output.
- Internal steps may change when the specified behavior stays the same.
Example: Price a Rental Through Two Procedures
An equipment desk bills rentals in started 20-minute blocks. Its software provides two procedures:
- blocks_for_minutes(minutes) accepts any nonnegative whole number of minutes. It returns the smallest nonnegative whole number of 20-minute blocks covering that duration. A zero-minute rental requires zero blocks.
- hire_cost_cents(minutes, rate) accepts the same duration and a positive whole-number rate in cents per block. It calls blocks_for_minutes(minutes) and returns the resulting block count multiplied by rate. There are no additional charges.
Neither procedure prints anything or changes information outside its local calculation. Here, observable behavior means the returned result and any stated effects a caller can detect.
A customer rents a tool for 74 minutes at 275 cents per block.
a) Find the return value of blocks_for_minutes(74).
First determine whether three blocks cover the rental. Three blocks cover 3×20 minutes, or ________, which is less than 74 minutes.
Four blocks cover 4×20 minutes, or ________. This covers the rental, and three blocks do not, so four is the smallest sufficient whole-number count.
The procedure returns 4 blocks. Its specification supplies enough information to find this result without knowing whether the implementation uses a loop or arithmetic.
b) Find the returned hire cost in cents and express the same charge in dollars.
The cost procedure uses the four-block result from the first procedure. To build the total, three blocks cost 3×275 cents, or ________. The fourth block adds another 275 cents.
Convert cents to dollars by dividing by 100:
The procedure returns 1100 cents, equivalent to $11.00. The caller receives the number in cents; converting it for display is a separate operation.
c) A programmer replaces the internal implementation of blocks_for_minutes with (minutes + 19) // 20, where // discards the remainder after whole-number division. The replacement has no side effects. Can callers continue to rely on the original specification?
Check useful boundary inputs first. At zero minutes, the numerator is ________, so the returned result is 19//20=0. At 60 minutes, the numerator is ________, so the returned result is 79//20=3. At 74 minutes, it gives 93//20=4.
These checks agree with the specification, but the replacement needs to work for every accepted duration. Write any accepted duration as m=20q+r, where q is a nonnegative integer and the remainder satisfies 0≤r<20.
If r=0, adding 19 leaves the whole-number quotient equal to q. The duration already fits exactly into q blocks, including the zero-duration case.
If 1≤r≤19, then 20≤r+19≤38. Whole-number division therefore adds exactly one to q, providing the extra block needed for the remaining minutes.
Thus the replacement always returns the smallest sufficient block count. It also preserves the absence of side effects. Yes, callers can continue to rely on the original specification. The internal method changes while the promised behavior stays the same.
A software team is documenting a library reservation component. Match each design term to the description that best identifies its role.
The operations available to callers, including their inputs, results and visible effects
Presenting values through permitted operations while hiding how those values are stored
The concrete structures and encodings used to store the component's values
Hiding the steps of a computation behind a callable operation with specified behaviour
Procedural abstraction
Data abstraction
Public interface
Internal representation
A parking payment program uses parking_cents(hours). For whole numbers of hours from 0 through 12, the procedure returns 180 * hours cents and has no side effects. Two vehicles park for three hours and two hours. The caller calculates their combined charge with a single 70-cent service fee:
cost = parking_cents(3)
total = cost + parking_cents(2) + 70What is the final value of total, in cents? Give an exact integer, to zero decimal places.