AP CSP topic 3.12
Calling Procedures
A procedure is a named group of instructions that can be used again and again. Calling a procedure runs its code. Arguments are the values you pass in when calling it, and they fill the procedure's parameters. Some procedures return a value back to the caller, which can be stored in a variable or used in an expression.
Calling Procedures vocabulary
- Procedure: A named group of instructions that may take parameters and return a value; also called a function.
- Procedure call: An instruction that runs a procedure.
- Argument: A value passed into a procedure when it is called.
- Return value: The value a procedure sends back to the code that called it.
AP-style practice questions
Try each one, then open the answer. To save your progress, take this quiz in the interactive course.
Skill: Code Analysis
PROCEDURE double(n)
{
RETURN (n * 2)
}
x ← double(5)
y ← double(x)
1. What is the value of y?
- (A) 5
- (B) 10
- (C) 20
- (D) 25
Show answer
(C) 20 x = 10, then double(10) = 20.
Skill: Abstraction in Program Development
2. In the call drawSquare(50, "blue"), what are 50 and "blue"?
- (A) Return values
- (B) Arguments
- (C) Comments
- (D) Variables that cannot change
Show answer
(B) Arguments Values passed into a procedure call are arguments.
Skill: Code Analysis
PROCEDURE greet(name)
{
DISPLAY(concat("Hi ", name))
}
3. Which of the following best describes what greet does?
- (A) It returns a value that can be stored
- (B) It displays a message but does not return a value
- (C) It changes the value of name everywhere in the program
- (D) It causes a syntax error
Show answer
(B) It displays a message but does not return a value The procedure has a DISPLAY but no RETURN.
Skill: Code Analysis
PROCEDURE f(a, b)
{
IF (a > b) { RETURN (a) }
RETURN (b)
}
4. What does f(3, 8) return?
- (A) 3
- (B) 8
- (C) 11
- (D) 24
Show answer
(B) 8 3 > 8 is false, so the procedure returns b, which is 8.