AP CSP Day 8: Procedures And Parameters
Share
A procedure (also called a function or subroutine) is a named block of code that can be called multiple times with different inputs called parameters. Parameters are local to the procedure and receive their values from the arguments passed during the call. AP CSP exam questions often test whether students understand that calling a procedure with specific arguments determines which code path executes. Procedures improve code reusability and reduce the need to repeat identical logic in multiple places.
📚 Study the Concept First (Optional) Click to expand ▼
Procedures and Parameters
What Is a Procedure?
A procedure is a named block of code that performs a specific task. Defining a procedure once lets you call it many times with different inputs, avoiding code repetition.
Parameters vs. Arguments
Parameters are the variable names listed in the procedure definition. Arguments are the actual values passed when calling the procedure. Arguments are copied into parameters when the call executes.
Practice Question
What is displayed after the following code runs?
PROCEDURE triple(n)
{
RETURN n * 3
}
result ← triple(4)
DISPLAY(result)The procedure triple receives 4 as the argument for parameter n. Inside the procedure, n * 3 = 4 * 3 = 12 is computed and returned. The returned value 12 is stored in result and displayed.
A) 4 is the input argument, not the return value. D) 7 would be n + 3, not n * 3. B) 3 is the multiplier, not the computed result.
Students confuse the input parameter value with the returned value, or they mix up addition and multiplication when reading the procedure body.
When tracing a procedure call, substitute the argument value into the parameter everywhere it appears in the body, then evaluate the expression to find the return value.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help