AP Computer Science Principles · Study guide

Big Idea 3: Procedures, Abstraction & The Robot

Big Idea 3 · Algorithms & Programming · Part 3 of 4

Procedures,
Abstraction & The Robot

How procedures package logic for reuse, how to work with unfamiliar procedures the exam gives you, and navigating the robot through grid diagrams.

§1 Procedures and Abstraction

A procedure is a named block of code that can be called with arguments. Procedures are the main tool for abstraction — packaging logic so the rest of the program can use it without knowing the details.

1.1 Defining and Calling

PROCEDURE double(n)
{
  RETURN(n * 2)
}

Call with double(5), which evaluates to 10.

1.2 Parameters vs. Arguments

Parameters are names in the definition (n above). Arguments are values passed when calling (5 above). The parameter takes on the argument's value inside the procedure.

1.3 RETURN vs. DISPLAY

  • RETURN sends a value back to the caller. It can be used in expressions: total ← double(7) + 1.
  • DISPLAY shows output on screen but returns nothing. total ← DISPLAY("hi") doesn't give a usable value.

1.4 Why Abstraction Helps

  • Reuse: write the logic once, call it many times.
  • Readability: computeTax(price) is clearer than 20 lines of tax math.
  • Fewer bugs: fix it in one place if the logic needs to change.
  • Separation: different developers can work on different procedures independently.
Exam tip

"Which benefit of using a procedure?" almost always rewards answers about reducing duplicated code or improving readability. Speed or performance is usually the wrong answer for this question type.

§2 Using Procedures Defined by the Question

A very common exam pattern: the question gives you 2–3 unfamiliar procedures, describes what each does, and asks you to pick or build an algorithm using them correctly.

Worked example

A question defines:

sort(list)Returns list sorted alphabetically.
combine(a, b)Returns a new list: entries of a followed by entries of b.
removeDups(list)Removes consecutive duplicate entries. Requires sorted list.

Task: count students on both the track team and band list.

bothList ← combine(trackList, bandList)
sortedList ← sort(bothList)
uniqueList ← removeDups(sortedList)
count ← LENGTH(bothList) − LENGTH(uniqueList)

A student on both lists appears twice in bothList. After sorting and deduplicating, duplicates are removed. The difference in length = the number of students on both.

Common pitfall

Always read each procedure's description precisely — especially requirements like "must be sorted first." The wrong answer choices often skip a required step or call procedures in the wrong order.

§3 The Robot

Several exam questions involve a robot navigating a grid. The robot is drawn as a triangle pointing in the direction it faces.

3.1 Robot Commands

CommandWhat it does
MOVE_FORWARD()Moves one square in the direction the robot faces.
ROTATE_LEFT()Turns 90° counterclockwise in place.
ROTATE_RIGHT()Turns 90° clockwise in place.
CAN_MOVE(direction)Returns true if an open square is one step in that direction (forward, backward, left, right — relative to facing).
Critical rule

If MOVE_FORWARD() is called when the square ahead is a wall (black) or off the grid, the robot stays in place and the program terminates. Use CAN_MOVE(forward) to check first.

3.2 Robot Example — Simple Navigation (No Obstacles)

No obstacles: the robot moves up 3, turns right, then moves right 3 to reach the gray goal square. Even with no obstacles, the grid diagram is always included.

3.3 Robot Example — Navigating Around Obstacles

Black cells at (3,2) and (3,3) block the direct path. The robot must go up 3 to clear the wall, then right 4 to reach the goal. A direct right-then-up path would crash into the wall and terminate the program.

3.4 Robot Example — Using CAN_MOVE in a Loop

For general-purpose navigation (regardless of obstacle placement), combine CAN_MOVE with REPEAT UNTIL:

REPEAT UNTIL(NOT CAN_MOVE(forward))
{
  MOVE_FORWARD()
}

This moves the robot forward until it hits something. Useful as a building block for wall-following algorithms.

Key idea

For robot questions, draw out the robot's path step by step on the grid. Track its current square and facing direction after every command. This catches mistakes like rotating the wrong direction or miscounting moves.

§4 Key Terms

TermDefinition
AbstractionHiding complexity behind a simpler interface (e.g., procedures)
ArgumentAn actual value passed into a procedure when called
ParameterName in a procedure definition that receives the argument's value
ProcedureA named block of code that can be called with arguments
RETURNSends a value back from a procedure to its caller

§5 Self-Check

  1. A robot is at (1,1) facing up. After: MOVE_FORWARD, MOVE_FORWARD, ROTATE_RIGHT, MOVE_FORWARD — where is the robot, and which direction is it facing?
  2. What is the benefit of writing code as a procedure rather than repeating the same lines throughout a program?
  3. A question defines a procedure max(a, b) that returns the larger of two values. Write pseudocode using max to find the largest of three values x, y, z.
Show Answers
  1. After 2 MOVE_FORWARDs: (1,3). After ROTATE_RIGHT: still (1,3), now facing right. After MOVE_FORWARD: (2,3) facing right.
  2. Reduces duplicated code, improves readability by giving a meaningful name to logic, and makes maintenance easier — one fix covers all call sites.
  3. biggest ← max(max(x, y), z) — first find the larger of x and y, then compare that to z.