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.
"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.
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.
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
| Command | What 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). |
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)
3.3 Robot Example — Navigating Around Obstacles
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.
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
| Term | Definition |
|---|---|
| Abstraction | Hiding complexity behind a simpler interface (e.g., procedures) |
| Argument | An actual value passed into a procedure when called |
| Parameter | Name in a procedure definition that receives the argument's value |
| Procedure | A named block of code that can be called with arguments |
| RETURN | Sends a value back from a procedure to its caller |
§5 Self-Check
- 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?
- What is the benefit of writing code as a procedure rather than repeating the same lines throughout a program?
- 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
- After 2 MOVE_FORWARDs: (1,3). After ROTATE_RIGHT: still (1,3), now facing right. After MOVE_FORWARD: (2,3) facing right.
- Reduces duplicated code, improves readability by giving a meaningful name to logic, and makes maintenance easier — one fix covers all call sites.
- biggest ← max(max(x, y), z) — first find the larger of x and y, then compare that to z.