Article
Using a procedure without knowing its insides
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:
PROCEDURE isEven(n)
{
RETURN(n MOD 2 = 0)
}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:
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
Your program
Calls isEven(7) and uses the true or false that comes back.
The procedure's name and description
isEven(n) gives back true if n is even.
The code inside
MOD, IF and RETURN. Hidden from the caller, who does not need to read it.
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
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
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
isEvencan be used again in a different program.
Fewer repeats, with parameters
Repeated code is a warning sign. Look at this program:
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:
PROCEDURE square(n)
{
RETURN(n * n)
}
DISPLAY(square(3))
DISPLAY(square(5))
DISPLAY(square(8))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:
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:
DISPLAY(4 * 2 + 1) DISPLAY(9 * 2 + 1) DISPLAY(20 * 2 + 1)
- Find what repeats: the calculation
* 2 + 1. - Find what changes: the first number, 4, 9 or 20. That is the parameter,
n. - Write the procedure:
PROCEDURE bonus(n)withRETURN(n * 2 + 1). - 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.