Home

Tutoring

Subjects

Live Classes

Study Coach

Essay Review

On-Demand Courses

Colleges

Games

Opening subject page...

Loading your content

  1. AP Computer Science Principles
  2. Calling Procedures

AP COMPUTER SCIENCE PRINCIPLES • ALGORITHMS AND PROGRAMMING

Calling Procedures

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

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.
✦ KEY TAKEAWAY
KEY TAKEAWAY
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)
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)
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(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.
Summary of procedure categories with example calls
CategoryParameters?Returns?Example Call
Action, No InputNoNogreet()
Value, No InputNoYesx ← pi()
Action, With InputYesNogreetUser("Ada")
Value, With InputYesYesarea ← square(7)
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

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

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

Step 3 — Compute sum inside addThenDouble

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

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

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.

Procedure benefits vs. limitations
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.
✦ KEY TAKEAWAY
KEY TAKEAWAY
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.

How procedure-calling concepts scale beyond AP CSP
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

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

PROBLEM 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?
PROBLEM 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?
PROBLEM 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?
PROBLEM 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)) }
PROBLEM 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.
SUMMARY

Calling Procedures — Summary

Varsity Tutors • AP Computer Science Principles • Calling Procedures