PrepGo

Libraries - AP Computer Science Principles Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 13 minutes to read.

Getting Started

Imagine building a complex robot from scratch. You could spend months designing and building every single component—every motor, sensor, and gear. Or, you could use pre-built, expertly-engineered parts. In programming, a library is like a collection of these pre-built parts, allowing you to build powerful, complex software efficiently without having to "reinvent the wheel" for every common task.

What You Should Be Able to Do

  • Explain how using a code library simplifies program development.

  • Identify the parts of a program that call a procedure from a library.

  • Use a library's documentation to write code that correctly calls a procedure.

  • Trace the flow of control when a program calls a procedure.

  • Explain the purpose of an Application Program Interface (API) in using libraries.

Key Concepts & Application

The Core Idea

At its heart, using a library is an act of abstraction. Abstraction is the process of simplifying complex reality by modeling classes of objects, events, or structures; it involves hiding the complex details and showing only the essential features. When you drive a car, you use the steering wheel, pedals, and gearshift. You don't need to know the details of internal combustion or the physics of the transmission. The car's controls provide an abstraction for the complex machinery underneath.

A software library works the same way. It contains a collection of pre-written and tested procedures that perform specific tasks. A procedure is a named group of instructions that accomplishes a specific task. For example, a math library might contain a procedure to calculate a square root, and a graphics library might contain procedures to draw shapes. You, the programmer, only need to know what the procedure does and how to call it, not the complex code that makes it work. This is called procedural abstraction, and it is a powerful tool for managing complexity in large programs.

To use a library, you rely on its Application Program Interface (API). An API is a set of specifications, including procedures and their documentation, that define how different software components can interact. Think of it as the user manual for the library. The API tells you the names of the available procedures, what inputs they need (parameters), and what results they produce (return values). Without clear documentation, a library would be a box of powerful tools with no instructions, making it nearly impossible to use correctly.

Logic & Application

When your program calls a procedure from a library, the normal, sequential flow of execution is interrupted. The program's control "jumps" to the library, executes the code inside the procedure, and then "returns" to the exact spot it left off in your code.

Example: Using a Drawing Library

Imagine we have a library named TurtleGraphics for drawing images with a virtual "turtle" on the screen. The library's API documentation might look like this:

Procedure NameParametersDescription
MOVE(distance)distance: a numberMoves the turtle forward the specified distance.
TURN(degrees)degrees: a numberTurns the turtle right by the specified degrees.
SET_COLOR(color)color: a text stringSets the drawing color (e.g., "red", "blue").
PEN_DOWN()(none)Puts the turtle's pen down to start drawing.
PEN_UP()(none)Lifts the turtle's pen up to stop drawing.

Now, let's write a program to draw a blue square with sides of length 100.

Annotated Pseudocode Example


// This program draws a 100x100 blue square using the TurtleGraphics library.


// 1. Set up the drawing color by calling a procedure from the library.

SET_COLOR("blue")


// 2. Prepare to draw.

PEN_DOWN()


// 3. Repeat the steps for each of the 4 sides of the square.

REPEAT 4 TIMES

{

    // 4. Call the MOVE procedure from the library to draw a side.

    MOVE(100)


    // 5. Call the TURN procedure from the library to turn at a corner.

    TURN(90)

}


// 6. Lift the pen when finished.

PEN_UP()

In this example, SET_COLOR("blue"), PEN_DOWN(), MOVE(100), TURN(90), and PEN_UP() are all procedure calls. They are the statements that invoke the pre-written code stored in the TurtleGraphics library.

Tracing & Analysis

Logic Trace

Let's trace the flow of control for the first few steps of the square-drawing program.

StepCurrent Line in ProgramActionFlow of Control Location
1SET_COLOR("blue")Program execution pauses.Main Program
2(inside library)Control jumps to the SET_COLOR procedure.TurtleGraphics Library
3(inside library)The library code to change the color runs.TurtleGraphics Library
4PEN_DOWN()Control returns to the next line.Main Program
5(inside library)Control jumps to the PEN_DOWN procedure.TurtleGraphics Library
6REPEAT 4 TIMESControl returns. Loop begins.Main Program
7MOVE(100)First iteration. Control jumps to MOVE.TurtleGraphics Library
8TURN(90)Control returns. Control jumps to TURN.TurtleGraphics Library
9MOVE(100)Control returns. Loop continues (2nd iter).Main Program

This trace shows how control is transferred back and forth between the main program and the library, allowing your simple program to leverage the library's complex functionality.

Core Concepts & Terminology

  • Library: A collection of pre-written and tested procedures that can be included and used in a program.

  • Procedure: A named group of instructions that can be called to perform a specific task. It is a fundamental building block of programs.

  • Procedural Abstraction: The practice of using a procedure by knowing what it does, without needing to know how it does it. This is the key benefit of using libraries, as it hides complexity.

  • Application Program Interface (API): A specification that describes how to interact with a library or other software component. It defines the available procedures, their parameters, and their expected behavior.

  • Documentation: The explanatory material that comes with a library or API, detailing how to use its procedures correctly.

  • Procedure Call: A statement in a program that executes a procedure. This action interrupts the sequential flow of the program and transfers control to the procedure.

    
    // A procedure call with one argument (parameter value)
    
    MOVE(100)
    
    
    // A procedure call with no arguments
    
    PEN_UP()
    

    This code does not define how to move or lift the pen; it simply invokes the existing procedures from a library to perform those actions.

Core Skill Check

  • Logic Tracing: A program uses a library with a procedure RANDOM(a, b) that returns a random integer between a and b, inclusive. What is the minimum possible final value of score after this code runs: score <- 0; REPEAT 2 TIMES { score <- score + RANDOM(1, 6) }?

  • Debugging: The API for a procedure is SET_POSITION(x, y), where x and y are numbers. Identify the error in the program statement SET_POSITION(50).

  • Application: Describe how a mobile app developer might use a location services library to add a map feature to their application.

Common Misconceptions & Clarifications

  • Confusing a Library and an API: The library is the actual code that does the work (the kitchen). The API is the set of rules and names you use to interact with that code (the menu).

  • Thinking You Need to Understand Library Code: The entire point of procedural abstraction is that you don't need to read or understand the library's internal code. You only need to read its API documentation.

  • Assuming All Libraries are Built-in: While programming languages come with standard libraries, many powerful libraries are developed by third parties and must be explicitly included in a project.

  • Calling a Procedure is the Same as Defining It: A procedure definition is the code that specifies what a procedure does. A procedure call is a single line of code that tells the program to run that defined procedure. When using a library, you only write calls; the definitions are already inside the library.

Summary

Software libraries are essential tools that make programming more efficient, reliable, and manageable. They are collections of pre-written, reusable procedures that perform common tasks. By using a library, programmers can leverage procedural abstraction, focusing on what they want to accomplish rather than the low-level details of how it is done. The API and its documentation provide the necessary instructions for calling library procedures. A procedure call temporarily transfers the program's flow of control to the library's code, which executes the task and then returns control to the main program, enabling complex functionality with minimal code.