AP Computer Science Principles · Unit 3: Algorithms & Programming
3.12 Calling Procedures
Calling a procedure
A procedure call (also called invoking a procedure) executes the statements inside that procedure. When a program calls a procedure, control transfers to the procedure's code, runs it, then returns to where it was called. Procedures can take parameters — inputs passed in when called — and may return a value using the parameter's actual arguments.
Parameters vs. arguments
A parameter is the placeholder variable listed in a procedure's definition. An argument is the actual value passed in when the procedure is called. For example, in `MOVE(distance)`, `distance` is the parameter; calling `MOVE(5)` passes the argument `5`. Multiple procedure calls can use the same procedure with different arguments to produce different results.
Why call procedures?
Calling existing procedures (built-in or programmer-defined) lets programmers reuse code without rewriting it, reducing complexity and errors. Procedure calls let a single block of abstracted code run multiple times with different arguments, supporting abstraction: the caller only needs to know what the procedure does and what inputs/outputs it uses, not how it works internally.
Key terms
- Procedure call
- An instruction that executes the statements inside a named procedure, then returns control to the calling code.
- Parameter
- A placeholder variable listed in a procedure's definition that receives an argument's value.
- Argument
- The actual value passed into a procedure when it is called.
- Code reuse
- Using existing procedures multiple times instead of rewriting code, reducing errors and complexity.
- Benefit of abstraction in calls
- The caller only needs to know what a procedure does and its inputs/outputs, not how it works internally.
Try an AP-style question
Code
PROCEDURE double(n)
{
RETURN(n * 2)
}
x ← double(double(3))What is x?
- 3
- 6
- 12
- 9
Show the answer
C. double(6).
Practice 8 AP-style questions with instant explanations, flashcards and spaced review.
Study 3.12 free in APrep