Procedural abstraction means you use a procedure by knowing what it does, its parameters and its return value, not how its code works. Parameters let one procedure handle many cases, and splitting a program into procedures makes it easier to test and change. In AP® Computer Science Principles, a library is a set of ready-made procedures, and its API documentation tells you how to call them.
Quick check
In PROCEDURE double(x) { RETURN(2 * x) }, what is x called, and what does double(7) return?Show answerHide answer
xis a parameter, anddouble(7)returns 14: the argument 7 is assigned tox, and 2 × 7 = 14.To call a library procedure whose code you cannot see, what must you know?Show answerHide answer
Its name, what it does, the parameters it takes and in what order, and what it returns. The library's documentation gives you all of these.
Know these already? Jump to the practice questions
What you'll be able to do
- Explain how abstraction lets you call a procedure without reading its code.
- Identify which values in duplicated code become parameters.
- Explain how modularity helps you test and fix a program.
- Predict what a library call returns from its documentation.
Key terms
Procedural abstraction
Using a procedure through its name, parameters and documented behavior, with the details of how it works hidden.
- In plain English
- You know what it does, so you don't need to know how.
- Don't write
- "Abstraction means the procedure has no code."
Modularity
Splitting a program into separate procedures, each doing one job.
- In plain English
- Building a program from small parts you can test one at a time.
- Don't write
- "Modularity means one long procedure."
Software library
A collection of procedures, already written and tested, that you can use in new programs.
- In plain English
- A toolbox of procedures you call instead of writing.
- Don't write
- "A library is a list of data values."
API (application programming interface)
The documented description of how a library's procedures behave and how to call them.
- In plain English
- The instruction manual for a library.
- Don't write
- "The API is the library's source code."
What does procedural abstraction hide?
When you call a procedure, you rely on three facts: its name, the parameters it expects, and what it does or returns. How the body works is hidden. That hiding is the abstraction: it cuts what you must hold in your head, so it helps manage a program's complexity.
Because callers depend only on what a procedure does, its body can change without breaking them. If average(numList) is rewritten to run faster but returns the same value for every list, no caller needs editing.
PROCEDURE, RETURN and procedure calls are on the exam reference sheet (see the note on procedures, parameters and return values). The terms abstraction, modularity, library and API are Learn this.
Key idea
To use a procedure, you need what it does, its parameters and its return value, never its code.
Check yourself
Show answerHide answer
No. The caller relies only on what sumList returns, and that hasn't changed.
How do parameters make one procedure general?
When two pieces of code are the same except for a value or two, those values should become parameters. One procedure then replaces every copy. Each call passes arguments, the values that are assigned to the parameters.
Suppose two code segments count the list values above 30 and above 25. Only the list and the limit differ, so both become parameters:
PROCEDURE countAbove(numList, limit)
{
count ← 0
FOR EACH num IN numList
{
IF(num > limit)
{
count ← count + 1
}
}
RETURN(count)
}
countAbove(temps, 30) and countAbove(temps, 25) now replace both segments. A parameter makes a procedure more general, so it solves a whole family of problems.
Check yourself
Show answerHide answer
limit. Arguments are matched to parameters by position, and 30 is the second argument.
Why split a program into procedures?
Modularity pays off in four ways:
- Readability: a call named
countAbovesays what happens in one line. - Reuse: you write the logic once and call it anywhere.
- Testing: you check each procedure on its own inputs first.
- Fixing: a bug is fixed in one place, and every call gets the fix.
Check yourself
Show answerHide answer
One: the procedure's body. All 12 calls then use the corrected code.
What do libraries and APIs give you?
A library saves you writing and testing procedures yourself. You use it through its API documentation, not its code. Each entry gives a procedure's name, its parameters in order, and what it returns. An entry for an invented procedure might read: clamp(value, low, high) returns low if value < low; otherwise high if value > high; otherwise value.
Documentation is how you know the parameter order, what comes back and what happens at the edges. Arguments match parameters by position, so clamp(30, 0, 20) returns 20 but clamp(0, 30, 20) returns 30.
Check yourself
Show answerHide answer
- The value −4 is below
low, which is 0, soclampreturns 0.
Worked example 1: Trace calls from the documentation
Question: A library's documentation says:
| Procedure | What it returns |
|---|---|
sumList(numList) | the total of the numbers in numList |
maxOf(a, b) | the larger of a and b |
clamp(value, low, high) | low if value < low; otherwise high if value > high; otherwise value |
What is displayed?
scores ← [12, 7, 15, 9]
best ← maxOf(scores[1], scores[3])
DISPLAY(clamp(best + 2, 0, 16))
DISPLAY(sumList(scores))
What tells you the method
Library calls with documentation but no code: evaluate each call from its documented return value.
Step 1: Find the arguments.
scores[1]is 12 andscores[3]is 15.WhyList indexes start at 1, so index 3 is the third element.
Step 2: Evaluate each call.
Call Returns maxOf(12, 15)15, so bestis 15clamp(17, 0, 16)16 sumList(scores)43 Why17 is above
high, soclampreturns 16; 12 + 7 + 15 + 9 = 43.
Check it
16 is within 0 to 16, and 43 exceeds every single score, so both are sensible.
Answer
16 43
Worked example 2: Turn duplicated code into a procedure
Question: Two copies of a loop count list values above a limit, one with 30 and one with 25. Design one procedure to replace both, then find what it returns for [28, 31, 25, 33] with limit 25.
What tells you the method
Repeated code that differs only in values: those values become parameters.
Step 1: State the input, output and header.
Inputs: a list and a limit. Output: how many elements exceed the limit. That gives
countAbove(numList, limit), whose body is in "How do parameters make one procedure general?".WhyThe inputs become the parameters, and the output becomes the return value.
Step 2: Trace it.
numnum > 25?count28 true 1 31 true 2 25 false 2 33 true 3 Why25 is not greater than 25, so it isn't counted.
Check it
With limit 30, only 31 and 33 count, giving 2, so the parameter works as designed.
Answer
3
Worked example 3: Choose the right call
Question: countAbove(numList, limit) returns how many elements of numList are greater than limit. The list scores holds whole numbers. Which statement displays how many scores are 70 or more?
- A)
DISPLAY(countAbove(scores, 70)) - B)
DISPLAY(countAbove(70, scores)) - C)
DISPLAY(countAbove(scores, 69)) - D)
DISPLAY(countAbove(scores, 71))
What tells you the method
A "which statement" question: trace each option on a test list that includes the boundary.
Step 1: Pick a test list.
Use
[70, 65, 88, 69, 71]: three scores (70, 88, 71) are 70 or more.WhyIncluding 70 itself tests the boundary.
Step 2: Trace each option.
A counts 88 and 71, giving 2. B passes the arguments in the wrong order. C counts 70, 88 and 71, giving 3. D counts only 88.
WhyFor whole numbers, "greater than 69" means the same as "70 or more".
Check it
On [69, 70], C returns 1, counting only 70, as it should.
Answer
C
Your turn
Question: countAbove(numList, limit) returns how many elements of numList are greater than limit. What is displayed?
temps ← [18, 25, 31, 25, 40]
hot ← countAbove(temps, 25)
DISPLAY(hot)
DISPLAY(countAbove(temps, hot))
Step 1:
Only 31 and 40 are greater than 25, so
hotis 2 and the first value displayed is 2.Step 2: Your go: evaluate
countAbove(temps, hot).
Show the full solutionHide the solution
Step 2. hot is 2, so the call is countAbove(temps, 2). All five temperatures are greater than 2, so it returns 5.
Answer: 2 5
Traps
Trap
You need to read a procedure's code before you can call it safely.
It seems right because code is what runs. But the name, parameters and documented return value are all you need, and the code can change without affecting you.
Can you correctly use a library procedure whose code you have never seen?
Yes, if its documentation tells you its parameters and what it returns.
Trap
The values written in a call are the parameters.
It seems right because both describe a procedure's inputs. But parameters are the variables in the header; the values in a call are arguments, which are assigned to the parameters.
In countAbove(temps, 30), is 30 a parameter or an argument?
An argument. It is assigned to the parameter limit.
What's examined
On the AP® Computer Science Principles exam, this topic belongs to Big Idea 3: Algorithms and Programming. Multiple-choice questions describe a procedure in words and ask what a call returns, which call completes a program, or why abstraction helps. Your Create program needs a procedure with a parameter, so these ideas also help you explain your own code.
Practice questions
- Recall Which statement best describes an API?
- A) The source code of every procedure in a library
- B) A list of the data values a program stores
- C) A program that translates pseudocode into a programming language
- D) Documentation of how a library's procedures behave and how to call them
Show answer
Answer: D. An API tells you each procedure's name, parameters and return value.
Why the others are wrong:
- A confuses the API with the code it hides.
- B describes data, not procedures.
- C describes a translator, not documentation.
- Apply
maxOf(a, b)returns the larger ofaandb.clamp(value, low, high)returnslowifvalue < low, otherwisehighifvalue > high, otherwisevalue. What doesclamp(maxOf(4, 9), 5, 8)return?
Show answer
Answer: 8
Marking points:
maxOf(4, 9)returns 9.- 9 is above
high, which is 8, soclampreturns 8.
- Apply A program contains this code segment, and a second copy that is identical except that 7 replaces 3.
k ← 1
REPEAT 5 TIMES
{
DISPLAY(3 * k)
k ← k + 1
}
Both are replaced by PROCEDURE showMultiples(n), with the same body except the DISPLAY line. Which DISPLAY line makes showMultiples(3) and showMultiples(7) display what the two copies display?
- A)
DISPLAY(n * k) - B)
DISPLAY(3 * k) - C)
DISPLAY(3 * n) - D)
DISPLAY(n * n)
Show answer
Answer: A. The 3 and the 7 are the only difference, so n replaces them: showMultiples(7) displays 7 14 21 28 35.
Why the others are wrong:
- B keeps the constant 3, so the parameter is never used.
- C replaces the wrong variable, so it displays 9 five times for
n= 3. - D drops
k, so it displays the same value five times.
- Apply
countAbove(numList, limit)returns how many elements ofnumListare greater thanlimit. What is displayed?
data ← [5, 12, 8, 15, 3]
DISPLAY(countAbove(data, 8))
DISPLAY(countAbove(data, 3))
Show answer
Answer: 2 4
Marking points:
- Above 8: 12 and 15, so 2. The 8 itself is not greater than 8.
- Above 3: 5, 12, 8 and 15, so 4.
- Exam-style A library's documentation says:
findIndex(aList, target)returns the index of the first element ofaListequal totarget, or 0 iftargetis not inaList. What is displayed?
names ← ["Ana", "Bo", "Cy", "Bo"]
DISPLAY(findIndex(names, "Cy"))
DISPLAY(findIndex(names, "Di"))
Show answer
Answer: 3 0
Marking points:
- "Cy" is the third element, and indexes start at 1, so the first call returns 3.
- "Di" is not in the list, so the documentation says the call returns 0.
- Exam-style A programmer rewrites the body of a library procedure so it runs faster. For every input it returns exactly what it returned before. Which statement is true?
- A) Every program that calls it must be rewritten.
- B) Callers keep working unchanged, because its name, parameters and return values are the same.
- C) Callers must now pass an extra argument.
- D) The API documentation must now list each step of the new code.
Show answer
Answer: B. Callers depend only on what the procedure does, which has not changed.
Why the others are wrong:
- A assumes callers depend on the hidden code.
- C: the parameters did not change.
- D: an API describes behavior, not the steps of the code.
Key points
Recall, then reveal
Say the main points from memory first, then check them.
Show key pointsHide key points
- Calling a procedure needs its name, parameters and return value, not its code.
- Values that differ between copies of code become parameters, and calls pass them as arguments.
- Arguments match parameters by position.
- Modularity makes programs easier to read, reuse, test and fix.
- A library holds ready-made procedures; its API documentation says how to call each one.