AP CSP
AP Computer Science Principles / All lessons / Big Idea 3: Algorithms and Programming
3Big Idea 3: Algorithms and Programming
AP CSP topic 3.13

Developing Procedures

Writing your own procedures is procedural abstraction: you give a task a name and use it without thinking about the details each time. Parameters make a procedure general, so one procedure like area(width, height) works for any rectangle. Breaking a big problem into smaller procedures makes code easier to read, test, and fix, and a change inside a procedure updates everywhere it is used.

Developing Procedures vocabulary

  • Procedural abstraction: Naming a process so it can be used by knowing what it does, not how.
  • Parameter: A variable in a procedure definition that receives an argument.
  • Modularity: Dividing a program into separate parts that each do one job.
  • RETURN: Ends a procedure and sends a value back to the caller.

AP-style practice questions

Try each one, then open the answer. To save your progress, take this quiz in the interactive course.

Skill: Abstraction in Program Development
PROCEDURE area(width, height)
{
   RETURN (width * height)
}

1. Which of the following is the main benefit of using parameters in this procedure?

  1. (A) The procedure runs faster
  2. (B) The same procedure can find the area of any rectangle
  3. (C) The procedure no longer needs to return a value
  4. (D) It prevents all logic errors
Show answer

(B) The same procedure can find the area of any rectangle Parameters generalize a procedure so it works for many inputs.

Skill: Abstraction in Program Development

2. A program draws 20 houses by repeating the same 15 lines of code for each one. Which change would best improve the program?

  1. (A) Write a procedure drawHouse(x, y) and call it 20 times
  2. (B) Copy the 15 lines 20 more times
  3. (C) Add more comments
  4. (D) Remove the houses
Show answer

(A) Write a procedure drawHouse(x, y) and call it 20 times Procedural abstraction removes repeated code and makes changes easier.

Skill: Abstraction in Program Development

3. A programmer fixes a bug inside a procedure that is called in 12 places. Which of the following is true?

  1. (A) The bug must be fixed 12 separate times
  2. (B) The fix applies everywhere the procedure is called
  3. (C) The procedure must be renamed
  4. (D) The program must be rewritten without procedures
Show answer

(B) The fix applies everywhere the procedure is called Changing the procedure updates every call, a key benefit of modularity.

Skill: Algorithms and Program Development

4. Which of the following procedures correctly returns true when n is a positive number and false otherwise?

  1. (A) PROCEDURE isPositive(n) { RETURN (n > 0) }
  2. (B) PROCEDURE isPositive(n) { RETURN (n < 0) }
  3. (C) PROCEDURE isPositive(n) { DISPLAY(n) }
  4. (D) PROCEDURE isPositive(n) { RETURN (n) }
Show answer

(A) PROCEDURE isPositive(n) { RETURN (n > 0) } n > 0 is a Boolean expression that is true exactly for positive numbers.

Open 3.13 in the interactive course