AP CSP
AP Computer Science Principles / All lessons / Big Idea 3: Algorithms and Programming
3Big Idea 3: Algorithms and Programming
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?

  1. (A) 5
  2. (B) 10
  3. (C) 20
  4. (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"?

  1. (A) Return values
  2. (B) Arguments
  3. (C) Comments
  4. (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?

  1. (A) It returns a value that can be stored
  2. (B) It displays a message but does not return a value
  3. (C) It changes the value of name everywhere in the program
  4. (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?

  1. (A) 3
  2. (B) 8
  3. (C) 11
  4. (D) 24
Show answer

(B) 8 3 > 8 is false, so the procedure returns b, which is 8.

Open 3.12 in the interactive course