← Back to course

Procedures & Abstraction

You’ll be able to

Procedures name a block of work

A procedure (also called a function or method) is a named group of programming instructions that can be called to run whenever needed. In AP pseudocode you define one as PROCEDURE name(parameter1, parameter2) { <instructions> }. A parameter is a variable listed in the definition that receives an input value; the actual value you pass in when calling is an argument. Calling name(3, 5) runs the body with parameter1 set to 3 and parameter2 set to 5. Procedures let you write a task once and reuse it many times.

RETURN sends a value back

A procedure can RETURN a value to whatever called it. RETURN(expression) immediately ends the procedure and hands that value back, so a call like total ← add(2, 4) runs add, gets its returned value (6), and stores it in total. Procedures that return values can be used inside larger expressions. Not every procedure returns something — some just perform an action, such as displaying text — but when one does, the call is that value.

Abstraction: hide the details

Abstraction is the practice of hiding complexity behind a simple interface, and procedures are the primary tool for it. Once computeTax(amount) exists, you call it by name without re-reading how it works every time — the details are hidden inside. This procedural abstraction lets you manage complexity: a big problem is broken into named sub-tasks, each understandable on its own, and the program reads as a description of what happens rather than a tangle of how. Good names make the abstraction meaningful.

Defining and calling a procedure
PROCEDURE square(n) { RETURN(n * n) } → result ← square(6) // result = 36
The parameter n receives the argument 6; RETURN sends back 36, which the call stores in result.
Worked example

Trace this program. What value is displayed? PROCEDURE combine(a, b) { RETURN(a * 2 + b) } x ← combine(3, 4) y ← combine(x, 1) DISPLAY(y)

  1. 1.First call combine(3, 4): a = 3, b = 4, so RETURN(3 * 2 + 4) = RETURN(10). Thus x = 10.
  2. 2.Second call combine(x, 1) = combine(10, 1): a = 10, b = 1, so RETURN(10 * 2 + 1) = RETURN(21). Thus y = 21.
  3. 3.DISPLAY(y) shows 21.
Answer: The program displays 21. The first call returns 10 into x, then the second call uses that 10 as its argument and returns 10 × 2 + 1 = 21.
Checkpoint

Given `PROCEDURE triple(n) { RETURN(n * 3) }`, what value does `result` hold after `result ← triple(4) + 2`?

Tip

When a call returns a value, mentally replace the call with its result, then finish the surrounding expression. triple(4) + 2 becomes 12 + 2 = 14. This substitution trick prevents most procedure-tracing mistakes.

Checkpoint

Which statement best describes the main benefit of using procedures (procedural abstraction) in a program?

On the exam

The exam frames procedures around abstraction and reuse (managing complexity), not speed. Know the vocabulary precisely: a parameter is in the definition, an argument is the value you pass in the call.

Answer the 2 checkpoints as you read.

Sign in to save your progress