AP Computer Science Principles • Algorithms and Programming

Calling Procedures

Learn how invoking named blocks of code enables abstraction, reuse, and modularity in programs.

Computer science principles illustration with a network, database, and code file
Inside this lesson 9 sections 5 questions 8 figures

SECTION 1

Historical Context & Motivation

Early computers were programmed by writing long, linear sequences of machine instructions. If you needed the same computation in two places, you copied every instruction by hand—an error-prone process that wasted precious memory. The desire to write a block of code once and invoke it many times drove the invention of what we now call procedures (also known as functions, subroutines, or methods). This single idea transformed programming from a craft of copying and pasting instructions into one of composing reusable abstractions.

1949 • EDSAC Subroutines

Maurice Wilkes and his team at Cambridge built the EDSAC computer and developed the first subroutine library, allowing commonly needed computations to be called from a shared catalog.

1958 • FORTRAN II Introduces SUBROUTINE

IBM's FORTRAN II formalized the CALL/RETURN mechanism with the SUBROUTINE and FUNCTION keywords, giving programmers high-level syntax for invoking reusable code blocks.

1972 • C Language & Function Calls

Dennis Ritchie's C language made function calls the fundamental unit of program organization, influencing virtually every modern language from Python to JavaScript.

2016 • AP CSP Exam Launches

The College Board introduces AP Computer Science Principles, placing procedure calls at the center of its Big Idea 3: Algorithms and Programming, using a language-agnostic pseudocode.

The fundamental question that procedures answer is straightforward yet profound: How can we name a sequence of steps, pass it specific data, and get a result back—without rewriting the steps every time? Understanding how to call procedures—and what happens when you do—is essential to reading, writing, and tracing any modern program.

SECTION 2

Core Principles & Definitions

Before exploring how procedure calls work in practice, it is important to establish clear terminology. The AP CSP exam uses a specific pseudocode notation, but the underlying ideas apply to every programming language.

1

Procedure (Function)

A named group of programming instructions that performs a specific task. A procedure may accept parameters and may return a value.
2

Procedure Call

The act of invoking a procedure by name, optionally supplying arguments. Execution jumps to the procedure body, runs it, then returns to the point of the call.
3

Parameters vs. Arguments

Parameters are the variable names listed in the procedure definition. Arguments are the actual values supplied when the procedure is called.
4

Return Value

The value a procedure sends back to the caller using a RETURN statement. Not all procedures return a value; those that don't simply perform an action (a side effect).
5

Procedural Abstraction

The principle that a caller needs to know what a procedure does, not how it does it. This hides complexity and allows collaboration.

SECTION 3

Visual Explanation: Anatomy of a Procedure Call

Flow of Execution When Calling a ProcedureCALLER (Main Program)x ← 5y ← 3result ← add(x, y)DISPLAY(result)↑ Execution resumes hereresult now holds 8Output: 8PROCEDURE add(a, b)Parameters receive arguments:a ← 5 (from x)b ← 3 (from y)sum ← a + bsum = 8RETURN(sum)Sends 8 back to callerCALLRETURN 8
The solid cyan arrow shows the moment execution jumps from the caller to the procedure body, passing arguments 5 and 3. The dashed emerald arrow shows the return path: the computed value 8 flows back and is stored in result.

The diagram above illustrates the two critical moments in every procedure call. First, the call transfers control from the main program to the procedure's body while binding the argument values to the parameter names. Second, the return sends the computed result back to the exact point of the call, and execution continues with the next statement in the caller. Notice how the caller never needs to know the internal variable sum—it only sees the returned value. This encapsulation is the essence of procedural abstraction.

SECTION 4

How Procedure Calls Work: AP CSP Pseudocode

The AP CSP exam reference sheet specifies two pseudocode patterns for defining and calling procedures. Understanding these patterns is essential for interpreting exam questions, which often present unfamiliar procedure definitions and ask you to trace their execution.

Defining a Procedure Without a Return Value

PROCEDURE DEFINITION (NO RETURN)

PROCEDUREname(param1,param2,…)<instructions>PROCEDURE name(param₁, param₂, …) { <instructions> }

The procedure is invoked by writing name(arg₁, arg₂, …). It performs its instructions (such as DISPLAY) but does not return a value to the caller.

Defining a Procedure That Returns a Value

PROCEDURE DEFINITION (WITH RETURN)

PROCEDUREname(param1,param2,…)<instructions>RETURN(expression)PROCEDURE name(param₁, param₂, …) { <instructions> RETURN(expression) }

The caller captures the returned value with an assignment: result ← name(arg₁, arg₂, …). The returned expression replaces the procedure call in the evaluation.

Calling a Procedure

PROCEDURE CALL SYNTAX

result←procedureName(arg1,arg2)result ← procedureName(arg₁, arg₂)

Arguments are matched to parameters by position: arg₁ maps to param₁, arg₂ maps to param₂, etc. The number of arguments must match the number of parameters.

ℹ
Exam Tip

SECTION 5

Types of Procedures & Their Behaviors

Procedures differ in whether they accept input and whether they produce output. The AP CSP exam tests all four combinations, so building an internal taxonomy helps you quickly identify what a given procedure does when you encounter it in a question.

Four Categories of ProceduresNo Return ValueReturns a ValueNo ParametersHas ParametersAction, No InputPROCEDURE greet(){ DISPLAY("Hello!") }Call: greet()Value, No InputPROCEDURE pi(){ RETURN(3.14159) }Call: x ← pi()Action, With InputPROCEDURE greetUser(name){ DISPLAY("Hi, " + name) }Call: greetUser("Ada")Value, With InputPROCEDURE square(n){ RETURN(n * n) }Call: area ← square(7)
The four quadrants show every combination of parameters (yes/no) and return values (yes/no). The most common AP CSP exam questions involve the bottom-right quadrant—procedures that accept parameters and return a value—because they require understanding both argument passing and return value capture.
CategoryParameters?Returns?Example Call
Action, No InputNoNogreet()
Value, No InputNoYesx ← pi()
Action, With InputYesNogreetUser("Ada")
Value, With InputYesYesarea ← square(7)

Summary of procedure categories with example calls

SECTION 6

Worked Example: Tracing a Multi-Procedure Program

The following pseudocode defines two procedures and a main program. Our goal is to trace the execution and determine the final displayed output—exactly the kind of question you will encounter on the AP exam.

ℹ
Code to Trace

Worked example

  1. 1

    Step 1 — Initialize variables

    The main program sets x ← 3 and y ← 4. At this point no procedure has been called yet.
    x = 3, y = 4
  2. 2

    Step 2 — Call addThenDouble(3, 4)

    Execution transfers to addThenDouble. The argument 3 is assigned to parameter a and 4 is assigned to b.
    a = 3, b = 4
  3. 3

    Step 3 — Compute sum inside addThenDouble

    The instruction sum ← a + b computes 3 + 4 = 7.
    sum = 7
  4. 4

    Step 4 — Nested call: double(7)

    Before addThenDouble can return, it calls double(sum) with sum = 7. Inside double, n receives 7 and the body computes 7 × 2 = 14.
    double returns 14
  5. 5

    Step 5 — Return chain and display

    The value 14 is returned from double to addThenDouble, which then returns 14 to the main program. The main program stores 14 in result and displays it.
    Output: 14

SECTION 7

Benefits & Limitations of Using Procedures

Using procedures is one of the most impactful design decisions in programming, but like any tool, it involves tradeoffs. The AP exam frequently asks why a programmer would—or would not—choose to use a procedure in a given scenario.

BenefitsLimitations / Considerations
Reusability: Write once, call many times. Reduces code duplication.Overhead: Each call adds a small runtime cost for transferring control and managing parameters.
Readability: A descriptive procedure name makes the caller's intent clear at a glance.Naming: A poorly named procedure can obscure meaning rather than clarify it.
Abstraction: Hides complexity; callers need not understand implementation details.Debugging: Errors inside a procedure can be harder to trace because the bug is separate from the call site.
Collaboration: Teams can develop procedures independently as long as the interface is agreed upon.Parameter mismatch: Passing the wrong number or type of arguments produces errors that may be difficult to diagnose.

Procedure benefits vs. limitations

SECTION 8

Connections to APIs, Libraries, and Beyond

The concept of calling procedures extends far beyond a single program. When you use a library function like math.sqrt() in Python or call a web API (Application Programming Interface) to retrieve weather data, you are performing the same fundamental operation: invoking a named procedure, passing arguments, and receiving a result. The only difference is that the procedure's body may reside in a different file, a different package, or even a different computer on the internet.

ConceptAP CSP ScopeBeyond AP CSP
Procedure callCall a named procedure with arguments; trace return valueCall stack management, recursion, tail-call optimization
Library useUse existing procedures without seeing source (e.g., ROBOT commands)Package managers (pip, npm), dependency management
API callUnderstand that APIs provide procedural interfaces to external servicesREST, GraphQL, authentication, rate limiting
Nested / composed callsEvaluate inner calls first, substitute return valuesHigher-order functions, callbacks, closures

How procedure-calling concepts scale beyond AP CSP

Mastering procedure calls at the AP CSP level builds the mental model you will rely on in every subsequent computer science course. Whether you are writing recursive algorithms in AP Computer Science A, building microservices in a software engineering course, or designing machine-learning pipelines, the pattern remains the same: define a clear interface, pass data in, and get results back.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

A programmer writes the following procedure: PROCEDURE mystery(a, b) { RETURN(a + b) } Which of the following best explains what happens when the statement result ← mystery(5, 3) is executed?
  • A. The values 5 and 3 are assigned to parameters a and b, the sum 8 is computed and returned, and 8 is stored in result.
  • B. The procedure displays the value 8 on the screen and stores nothing in result.
  • C. The variables a and b are permanently changed to 5 and 3 in the main program.
  • D. The procedure runs but result remains undefined because RETURN only works with one parameter.
Show answer (A)
A is correct. When mystery(5, 3) is called, 5 is assigned to parameter a and 3 to parameter b. The body computes a + b = 8 and returns it. The assignment result ← mystery(5, 3) stores 8 in result. B is wrong because RETURN sends the value back to the caller—it does not display anything. C is wrong because a and b are local to the procedure; they do not exist in the main program scope. D is wrong because RETURN can accept any expression, including one computed from two parameters.

Question 2: BASIC CALCULATION

Consider the following code: PROCEDURE triple(x) { RETURN(x * 3) } a ← 4 b ← triple(a) c ← triple(b) What is the value of c after this code executes?
  • A. 12
  • B. 36
  • C. 9
  • D. 48
Show answer (B)
B is correct. First, triple(4) returns 4 × 3 = 12, so b = 12. Then triple(12) returns 12 × 3 = 36, so c = 36. A (12) is the value of b, not c—a common mistake if you stop one call too early. C (9) would result from tripling 3, not 4. D (48) would result from computing 4 × 12, which is not what the procedure does.

Question 3: INTERMEDIATE

Consider the following two procedures and program: PROCEDURE add1(x) { RETURN(x + 1) } PROCEDURE doubleIt(x) { RETURN(x * 2) } result ← doubleIt(add1(3)) Which TWO of the following statements are true about the execution of this code?
  • A. add1(3) is evaluated first, returning 4, which is then passed as the argument to doubleIt.
  • B. doubleIt is called first with argument 3, and its return value is passed to add1.
  • C. The value of result is 8.
  • D. The value of result is 7.
Show answer (A,C)
A and C are correct. When procedure calls are nested, the innermost call is evaluated first. add1(3) returns 3 + 1 = 4. That return value (4) becomes the argument to doubleIt, which returns 4 × 2 = 8. B is wrong because it reverses the evaluation order—inner calls are evaluated before outer calls. D (7) would come from computing 3 × 2 + 1, which implies doubleIt ran first and add1 ran second, which contradicts how nested calls work.

Question 4: APPLIED

A student is building a program to compute the total cost of items in an online shopping cart. The procedure tax(price) returns the price with 8% sales tax added. The procedure discount(price, pct) returns the price after subtracting pct percent. The student wants to apply a 10% discount first, then add tax. Which of the following correctly computes the final price for an item costing $50? PROCEDURE tax(price) { RETURN(price * 1.08) } PROCEDURE discount(price, pct) { RETURN(price * (1 - pct / 100)) }
Show answer
Sample correct answer: finalPrice ← tax(discount(50, 10)) Rubric point 1: The inner call discount(50, 10) correctly applies the 10% discount: 50 × (1 − 10/100) = 50 × 0.90 = 45. Rubric point 2: The return value 45 is passed directly as the argument to tax, demonstrating correct nesting of procedure calls. Rubric point 3: tax(45) returns 45 × 1.08 = 48.60, which is stored in finalPrice. Note that reversing the order (discount(tax(50), 10)) would yield a different result (48.60 vs. 48.60 in this case due to commutativity of multiplication, but the problem specifies discount first, then tax, which is the standard business convention).

Question 5: CRITICAL THINKING

A team of programmers is developing a robot navigation system. Programmer A writes a procedure moveForward(steps) that makes the robot move the given number of steps. Programmer B writes a procedure turnRight(degrees) that rotates the robot. Neither programmer has seen the other's code. Programmer C writes the main program that calls both procedures to navigate a square path. Explain how procedural abstraction enables this collaboration. In your answer: (a) Identify the information Programmer C needs about each procedure. (b) Identify the information Programmer C does NOT need. (c) Explain one specific benefit of this arrangement. (d) Describe one risk if the procedure interfaces are not clearly documented.
Show answer
Sample response: (a) Programmer C needs to know each procedure's name, the parameters it accepts (moveForward requires a numeric steps value; turnRight requires a numeric degrees value), and whether it returns a value or simply performs an action. With this information, C can write calls like moveForward(10) and turnRight(90). (b) Programmer C does not need to know how the procedures work internally—for example, how moveForward converts a step count into motor voltages, or how turnRight calculates servo positions. This internal logic is hidden by procedural abstraction. (c) One key benefit is parallel development: Programmer A can optimize moveForward independently (e.g., adding obstacle detection) without requiring any changes to Programmer C's main program, as long as the interface (name, parameters, behavior) remains the same. (d) If the interface is not clearly documented, Programmer C might pass arguments in the wrong units—for instance, passing radians to turnRight when it expects degrees. This would cause the robot to turn by the wrong amount, producing incorrect navigation without any explicit error message, making the bug difficult to diagnose.

Varsity Tutors • AP Computer Science Principles • Calling Procedures