Unit 3: Programming · Procedural abstraction and libraries

Article

Using a procedure without knowing its insides

7 min read

You press "popcorn" on a microwave. You do not know how it decides the power or the time. You only know what the button does, and that is enough to use it. Programmers use procedures in the same way.

Use a procedure without opening it

Abstraction means hiding the details of how something works, so you can think about what it does. Procedural abstraction means giving a process a name (a procedure) so that you can use it while knowing only what it does, not how it does it.

Take the procedure isEven(n). It gives back true if n is even and false if not. Here are two different ways to write the inside:

AP pseudocode
PROCEDURE isEven(n)
{
    RETURN(n MOD 2 = 0)
}
AP pseudocode
PROCEDURE isEven(n)
{
    IF(n MOD 2 = 0)
    {
        RETURN(true)
    }
    RETURN(false)
}

The two versions look different, but they do the same job. Run both in Python and compare:

Runs in your browser

The output is True True and then False False. A programmer who writes isEven(7) does not care which version is inside. They only need the name and what it does.

What each part needs to know

  1. Your program

    Calls isEven(7) and uses the true or false that comes back.

  2. The procedure's name and description

    isEven(n) gives back true if n is even.

  3. The code inside

    MOD, IF and RETURN. Hidden from the caller, who does not need to read it.

The caller depends only on the middle layer. The code inside can be rewritten without the caller noticing.

This is powerful. If someone finds a bug in isEven, or writes a faster version, they change the code inside the procedure. Every program that calls it keeps working, as long as what the procedure does stays the same.

Check your understanding

Mia's teammate wrote a procedure isPrime(n) that returns true if n is a prime number. What does Mia need to know in order to use it?

Small parts of a big problem

Big programs are hard to understand all at once. So programmers split them into smaller problems and give each one a procedure. This is called modularity: dividing a program into separate parts (modules) that can each be written, tested and fixed on their own.

A quiz game split into procedures

Quiz game
  • showQuestion

    • displays the next question
  • checkAnswer

    • says if the answer was right
  • updateScore

    • adds a point for a right answer
  • showResult

    • displays the final score
Each procedure is a small problem with one clear job. The main program just calls them in order.

Splitting a program this way has several benefits:

  • Easier to understand, because each part is small and has a name.
  • Easier to test and fix. If the score is wrong, you look in updateScore.
  • Teamwork. Different people can write different procedures at the same time.
  • Reuse. A procedure such as isEven can be used again in a different program.

Fewer repeats, with parameters

Repeated code is a warning sign. Look at this program:

AP pseudocode
DISPLAY(3 * 3)
DISPLAY(5 * 5)
DISPLAY(8 * 8)

The three lines are almost the same. Only the number changes. The part that changes becomes a parameter. The part that stays the same goes in the body:

AP pseudocode
PROCEDURE square(n)
{
    RETURN(n * n)
}
DISPLAY(square(3))
DISPLAY(square(5))
DISPLAY(square(8))
Runs in your browser

Both programs display 9, 25 and 64. The second one has the multiplication written once. Because the procedure has a parameter, it works for any number, not only 3, 5 and 8. We say the parameter generalises the procedure.

Check your understanding

A program contains these three lines:

AP pseudocode
DISPLAY(10 * 10 * 3)
DISPLAY(20 * 20 * 3)
DISPLAY(35 * 35 * 3)

Which change removes the repeated code and still displays the same three values?

Worked example

A shop gives a bonus: double the points and add 1. The program shows the bonus for three customers:

AP pseudocode
DISPLAY(4 * 2 + 1)
DISPLAY(9 * 2 + 1)
DISPLAY(20 * 2 + 1)
  1. Find what repeats: the calculation * 2 + 1.
  2. Find what changes: the first number, 4, 9 or 20. That is the parameter, n.
  3. Write the procedure: PROCEDURE bonus(n) with RETURN(n * 2 + 1).
  4. Replace each line: DISPLAY(bonus(4)), DISPLAY(bonus(9)), DISPLAY(bonus(20)).

The output is 9, 19 and 41, the same as before. Now the shop changes the rule to "triple the points and add 1". In the first program, you must edit three lines and you might miss one. In the second program, you edit one line inside bonus, and every call is correct.

Common mistakes

  • "To use a procedure, I must read all of its code." No. You need to know what it does, which its name and description tell you.
  • "Abstraction makes a program run faster." No. It makes a program easier for people to write, read and change. Speed is a different matter.
  • "If I change how a procedure works, I must change every call." Not if what it does stays the same.
  • "A procedure with a parameter works for only one value." The reason to add a parameter is so it works for many values.
  • "Procedures always make a program shorter." Not always. They make it easier to manage, and they remove repeated code.

On the exam

Questions ask for the benefit of using a procedure. The correct answers are about managing complexity: removing repeated code, splitting a big problem into small ones, letting several programmers work at the same time, and letting you use a procedure without knowing how it works. Be careful with answers that say "faster" or "uses less memory". They are not the reason for procedural abstraction.

Key words

Abstraction: hiding details so you can focus on what something does.

Procedural abstraction: giving a process a name, so it can be used knowing only what it does.

Modularity: dividing a program into separate parts, each with its own job.

Generalise: make a procedure work for many values by using a parameter.

Duplication: the same code repeated in several places.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.