Abstraction CS Thinking Example 2

Follow the full solution, then compare it with the other examples linked below.

Example 2

medium
A function `calculateArea(length, width)` returns `length * width`. Explain how this is an example of abstraction.

Solution

  1. 1
    Step 1: The user calls `calculateArea(5, 3)` and gets 15 โ€” they do not need to know how multiplication is implemented.
  2. 2
    Step 2: The function hides the implementation detail (the multiplication) behind a meaningful name.
  3. 3
    Step 3: If the formula changed (e.g., adding units conversion), the caller's code would not need to change.

Answer

The function abstracts away the calculation detail, exposing only inputs and output.
Functions are a primary tool for abstraction in programming. They encapsulate complexity behind a simple interface, making code easier to read and maintain.

About Abstraction

Focusing only on the essential information needed to solve a problem while ignoring irrelevant details. Abstraction reduces complexity by creating simplified models that capture what matters and hide what does not, enabling reasoning at higher levels.

Learn more about Abstraction โ†’

More Abstraction Examples