AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/3/3-13)
Unit 3 · Topic 3.13
3.13 Developing Procedures
Writing your own procedures is how you break a big program into manageable parts. This topic covers procedural abstraction, modularity and using parameters to make one procedure work for many cases, all of which you'll need to explain about your own Create task.
Key terms
- procedural abstraction
- modularity
- parameter
- RETURN
- reusing code
Procedural abstraction
Procedural abstraction means giving a process a name so it can be used by knowing only what it does, not how it does it. You call LENGTH(aList) without ever seeing its code. Your own procedures work the same way for anyone who calls them.
Because callers depend only on what a procedure does, you can change how it works inside (make it faster or use less memory) without changing any of the code that calls it, as long as it still does the same job.
Modularity: solving big problems in pieces
Procedural abstraction lets you solve a large problem by solving smaller subproblems, each with its own procedure. Splitting a program into separate subprograms like this is called modularity.
A quiz game might have askQuestion, checkAnswer, updateScore and showResults. Each one is short enough to write, test and fix on its own, and the main program reads like an outline.
Generalizing with parameters
When you see nearly identical code repeated, pull out what's shared into a procedure, and turn what changes into a parameter:
DISPLAY("Welcome,")
DISPLAY("Ana")
DISPLAY("Welcome,")
DISPLAY("Ben")
DISPLAY("Welcome,")
DISPLAY("Cho")
The three pairs of lines differ only in the name, so pull the shared part into a procedure and make the name a parameter:
PROCEDURE welcome(name)
{
DISPLAY("Welcome,")
DISPLAY(name)
}
Now welcome("Ana"), welcome("Ben") and welcome("Cho") display exactly the same thing as the six lines above, and the procedure works for any name, not just these three. That's code reuse: write once, call many times. It shortens the program and means a fix in one place fixes every use. To change the greeting to "Hi," you'd edit one line instead of three.
Parameters are what make a procedure general. Here's one that keeps a value inside a range, for any range:
PROCEDURE limit(value, low, high)
{
IF (value < low)
{
RETURN(low)
}
IF (value > high)
{
RETURN(high)
}
RETURN(value)
}
limit(-5, 0, 100) returns 0, limit(250, 0, 100) returns 100 and limit(42, 0, 100) returns 42. The same code could limit a volume setting, a game character's position or a percentage.
Why it all helps
Procedural abstraction manages complexity: programs are easier to read (a well-named call says what's happening), easier to test (each piece separately), easier to fix (one place to change) and easier to reuse.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Write a procedure with a parameter
A teacher's program repeats the same eight lines three times to compute the average of three different classes' score lists. Write one procedure that replaces them, and show how to call it.
Show the solutionHide the solution
- Step 1: What changes between the three copies? Only which list is averaged. That becomes the parameter.
- Step 2: Inside: add up the elements with a FOR EACH loop, then divide by the length.
- Step 3: Return the result so the caller can display or store it:
PROCEDURE classAverage(scores) { total ← 0 FOR EACH s IN scores { total ← total + s } RETURN(total / LENGTH(scores)) } - Step 4: Call it once per class, such as
avg1 ← classAverage(period1). For [90, 80, 85] it returns 85; for [70, 75] it returns 72.5.
Answer: A
classAverage(scores)procedure that totals the list and returns the total divided byLENGTH(scores), called once for each class's list. - Example 2
Explaining how a procedure manages complexity
In a fitness app, the procedure
caloriesBurned(minutes, activity)is called from six places. Explain how it manages complexity.Show the solutionHide the solution
- Step 1: Say what it replaces: without it, the calorie calculation would be copied into all six places.
- Step 2: Say what it makes easier: if the formula changes, it's fixed in one place instead of six, and there's no risk of the copies drifting apart.
- Step 3: Mention the parameters: because minutes and activity are parameters, the same code works for every workout, so no separate version is needed for each activity.
Answer: The calculation is written once and reused through calls with different arguments, so the program is shorter, any fix happens in one place, and one procedure handles every activity.
Common mistakes
- Explaining procedural abstraction as "it makes the code organized" without saying how. Name the repeated code it replaces or the change it makes easier.
- Hard-coding values inside a procedure that should be parameters, so it only works for one case.
- Thinking the caller needs to know how a procedure works inside. It only needs to know what it does.
On the exam
- Your Create task needs a student-developed procedure with at least one parameter that affects what it does, and an algorithm inside that uses sequencing, selection and iteration. Written Response 2(c) can ask how it manages complexity.
- Multiple-choice questions may ask which procedure correctly generalizes repeated code, or what a change inside a procedure would affect.
Connected topics
Videos
Check yourself
4 questions on 3.13 Developing Procedures. Pick an answer to see if you got it, and why.
Which procedure correctly returns the number of elements in aList that are greater than limit? For example, countOver([3, 9, 5, 12], 5) should return 2.
A team's program calls the procedure sortScores(scores) in twelve places. A programmer rewrites the code inside sortScores so that it runs faster, but it still takes the same input and returns the same sorted list. What must change in the rest of the program?
A program has three nearly identical code segments. One draws a square with sides of 10, one with sides of 20 and one with sides of 30; they differ only in that number. Which change best uses procedural abstraction?
A large program is split into several procedures, each handling one part of the task. Which two of the following are benefits of this? Select two answers.
Select two answers. 0 of 2 chosen
0 of 4 answered