Calling Procedures (College Board AP® Computer Science Principles): Revision Note
Procedure syntax and parameters
What is a procedure?
A procedure is a named group of instructions that performs a specific task
Procedures are also referred to as methods or functions in different programming languages
Using procedures avoids rewriting the same code multiple times and makes programs easier to read and maintain
Calling a procedure
A procedure call executes the instructions inside the procedure
In AP CSP pseudocode, a procedure is called by writing its name followed by parentheses
PROCEDURE greet()
{
DISPLAY("Hello!")
}
greet()
Calling
greet()executes the DISPLAY instruction and outputs "Hello!"
Parameters and arguments
A parameter is a variable in the procedure definition that receives a value when the procedure is called
An argument is the actual value passed into the procedure during the call
Parameters allow the same procedure to work with different inputs each time it is called
A procedure can also collect input by calling
INPUT()inside its body instead of receiving values through parameters
PROCEDURE greet(name)
{
DISPLAY(CONCAT("Hello, ", name))
}
greet("Alex")
greet("Sam")
nameis the parameter;"Alex"and"Sam"are the argumentsThe procedure displays a different greeting depending on the argument passed
Return values and control flow
What is a return value?
A return value is a value that a procedure sends back to the part of the program that called it
Not all procedures return a value; some only perform an action such as displaying output
When a procedure returns a value, it can be stored in a variable or used directly in an expression
The
RETURNstatement ends the procedure and sends a value back to the callerWhen
RETURNis reached, the procedure stops executing immediately and no further statements in the procedure run
PROCEDURE square(n)
{
RETURN(n * n)
}
result ← square(5)
DISPLAY(result)
This stores 25 in
resultand displays it
Sequence interruption
Calling a procedure temporarily interrupts sequential flow; the program jumps to the procedure body, executes it, then resumes at the point of the call
The effect on the calling code depends on whether the procedure returns a value:
Procedure type | Has RETURN? | Effect on calling code |
|---|---|---|
Action only | No | Executes and returns; calling code continues |
Value-returning | Yes | Returns a value; calling code uses it |
Examiner Tips and Tricks
In code trace questions, track the argument values passed to each procedure call separately — a procedure called multiple times with different arguments produces different outputs each time.
For the CPT, your program must include a student-developed procedure with a parameter that is used within the procedure body; make sure you can explain what the parameter does in your written response.
Worked Example
What does the following code segment display?
PROCEDURE double(x)
{
RETURN(x * 2)
}
a ← double(3)
b ← double(a)
DISPLAY(b)
(A) 3
(B) 6
(C) 12
(D) 24
[1]
Answer:
(C) 12 [1 mark]
double(3)returns 6 and assigns it toa;double(a)then passes 6 as the argument, returning 6 × 2 = 12, which is displayed
Unlock more, it's free!
Was this revision note helpful?