---
title: "Libraries | FiveStudy"
description: "What software libraries and APIs are, why documentation is necessary, and how reusing tested procedures makes complex programs easier to create."
url: https://fivestudy.com/guides/ap-csp-3-14-libraries
---

[AP Computer Science Principles](https://fivestudy.com/ap-computer-science-principles) · Study guide

# Libraries

[Figure: FiveStudy]

FiveStudy

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

AP® Computer Science Principles · Big Idea 3 · Algorithms and Programming

# Libraries

What a software library is, what an API tells you, why documentation matters, and how borrowing tested code makes big programs easier to build.

1. Borrowing code that already works 2. Where existing code comes from 3. The API and its documentation 4. Same job, two ways 5. When you guess instead of reading 6. Worked examples 7. Common mistakes

## 1 Borrowing code that already works

A **software library** is a collection of procedures that can be used in creating new programs. Someone wrote them, tested them, and packaged them so other programs can call them. When a program needs a square root, a sort, a map, or today's weather, it usually calls a library instead of writing that code again.

Using libraries **makes complex programs easier to create**. You write less code, you test less code, and the borrowed parts have already been checked by many other programs. A library saves the programmer's effort; it does not automatically make the program run faster.

## 2 Where existing code comes from

Existing code segments can come from **internal** or **external** sources:

| Source | Examples |
| --- | --- |
| Internal: code you or your team already wrote | a procedure from last month's project; a shared folder of your company's tested procedures |
| External: code from outside | a library that comes with the programming language; a free library someone published online; a map or payment service's library |

Either way, the code was written and tested before your program needed it. When you use code someone else wrote, give credit to its author and follow the rules (the license) it was shared under.

## 3 The API and its documentation

An **API** (application program interface) is a specification for how the procedures in a library **behave** and how they **can be used**: each procedure's name, what to pass in and in what order, what comes back, and what happens in unusual cases. The API is written down in the library's **documentation**.

**Figure 1.** An API describes how each library procedure behaves and how to call it. You write your program against that description, so the code inside can stay out of sight.

Here is the documentation for a small library of list procedures used on the rest of this page:

| Procedure | Takes | Returns | Edge cases |
| --- | --- | --- | --- |
| `largest(aList)` | a list of numbers | the largest value in the list (the value, not its position) | an empty list causes an error and the program stops |
| `average(aList)` | a list of numbers | the total divided by how many numbers there are; may be a decimal | an empty list causes an error and the program stops |
| `countAbove(aList, limit)` | a list of numbers, then one number | how many items are greater than `limit` (items equal to it are not counted) | returns 0 for an empty list |

Documentation is **necessary**, not just helpful. You usually cannot see the library's code, and even if you could, reading it would cost the time the library was supposed to save. The documentation is how you learn that `largest` returns a value and not a position, or that `countAbove` skips items equal to the limit.

## 4 Same job, two ways

A teacher wants the top score, the average, and how many scores are above 80. With the library, the program is 4 lines:

```
scores ← [72, 88, 95, 61, 84]
DISPLAY(largest(scores))
DISPLAY(average(scores))
DISPLAY(countAbove(scores, 80))
```

Without it, the program has to do all of that work itself, in 19 lines:

```
scores ← [72, 88, 95, 61, 84]
best ← scores[1]
total ← 0
count ← 0
FOR EACH s IN scores
{
  IF (s > best)
  {
    best ← s
  }
  total ← total + s
  IF (s > 80)
  {
    count ← count + 1
  }
}
DISPLAY(best)
DISPLAY(total / LENGTH(scores))
DISPLAY(count)
```

Both programs display `95 80 3`. The library version is shorter, easier to read, and leaves fewer places for a mistake to hide, because the work inside `largest`, `average`, and `countAbove` was already written and tested. This is procedural abstraction on a bigger scale: you use what a procedure does without needing to know how it does it.

The key idea

A library is a collection of tested procedures to use in new programs, from internal or external sources. Its API specifies how each procedure behaves and how to call it, and the documentation is how you learn that. Libraries make complex programs easier to create.

## 5 When you guess instead of reading

A student guesses that `largest` returns the *position* of the largest score and writes:

```
scores ← [72, 88, 95, 61, 84]
top ← largest(scores)
DISPLAY(scores[top])
```

`largest(scores)` returns 95, the value. The program then asks for `scores[95]`, and the list has only five items, so the program stops with an error: index 95 is out of range for a list of 5 items. One look at the documentation (“the value, not its position”) would have led to `DISPLAY(top)`, which displays 95.

## 6 Worked examples

Worked Example 1 Use the library

### Warm days

Using the documented procedures, what does this display?

```
temps ← [58, 64, 71, 69, 75, 80, 77]
DISPLAY(countAbove(temps, 70))
DISPLAY(largest(temps))
```

1. 1

   `countAbove(temps, 70)` counts the items greater than 70. Check each one: 71, 75, 80, and 77 are; 58, 64, and 69 are not. The count is 4.

2. 2

   `largest(temps)` returns the largest value, 80.

**Answer:** `4 80`.

Worked Example 2 Read the documentation

### Edge cases

Using only the documentation table, predict each result: (a) `countAbove([3, 8, 8, 10], 8)`; (b) `average([10, 15])`; (c) `countAbove([], 5)`.

1. 1

   (a) Only items *greater than* 8 count, so the two 8s do not. That leaves 10 alone: 1.

2. 2

   (b) (10 + 15) ÷ 2 = 12.5; the documentation says the result may be a decimal.

3. 3

   (c) The documentation says an empty list gives 0.

**Answer:** (a) 1, (b) 12.5, (c) 0.

Worked Example 3 Avoid a documented error

### No ages yet

The documentation says `average` of an empty list causes an error. Rewrite this segment so it never stops with an error.

```
ages ← []
DISPLAY(average(ages))
```

1. 1

   Check the list's length before calling the procedure.

2. 2

   Only call `average` when there is at least one item:

   ```
   ages ← []
   IF (LENGTH(ages) > 0)
   {
     DISPLAY(average(ages))
   }
   ELSE
   {
     DISPLAY("no ages entered")
   }
   ```

**Answer:** With an empty list it displays `no ages entered`; with `[15, 16, 17]` it displays `16`.

Worked Example 4 Explain the benefit

### Why use the library?

A classmate says: “Using a library is cheating, and it makes the program slower because it has to call other code.” Respond.

1. 1

   Using tested procedures from a library is normal practice; the library exists to be used in new programs (with credit to its authors).

2. 2

   The benefit is about building the program: less code to write and test, and fewer places for errors.

3. 3

   A library is not automatically faster or slower; speed depends on the algorithms inside, not on whether the code was borrowed.

**Answer:** Libraries make complex programs easier to create by reusing tested procedures; they are a development benefit, not a speed guarantee.

## 7 Common mistakes

Common Mistake 1 · Guessing what a procedure returns

A name like `largest` doesn't say whether you get a value or a position. Read the documentation.

Common Mistake 2 · Ignoring the edge cases

Documentation often lists inputs that cause errors, such as an empty list. Check for them before you call.

Common Mistake 3 · Swapping the arguments

`countAbove(80, scores)` passes the number where the list should go, and the program fails. The documentation gives the order.

Common Mistake 4 · Thinking a library makes the program faster

The benefit is easier development: less code to write and test. Speed depends on the algorithm inside.

Explain a library like this

“The program uses a library procedure, `largest`, instead of writing its own loop. The library's documentation describes what the procedure takes and returns, so the programmer can use tested code without reading how it works, which makes the program shorter and easier to build.”

Name the procedure, say what its documentation tells you, and say how that simplifies writing the program.

FiveStudy AP® Computer Science Principles · Libraries
