AP® Computer Science Principles · Big Idea 3 · Topic 3.14
Libraries and APIs
A library is a pre-written collection of procedures, and an API is the documented list of what each procedure does and how to call it, without exposing how it works inside.
Nobody writes their own square root routine because somebody already wrote one, tested it, and packaged it into a library. A library is just a collection of procedures someone else built, ready to be called. The API, or application programming interface, is the public face of that library: the names of its procedures, what inputs they expect, and what output they return. It tells you what each procedure does without showing you the code that does it. That hidden inner code is called abstraction, the same idea behind procedures in general, just scaled up to code other people wrote.
Because the API only promises behavior, the library's internals can change freely. If the maintainers rewrite sqrt to use a faster algorithm next year, your program calling √25 still gets 5.0 back. You never touch their code, so their rewrite does not break yours, as long as the documented inputs and outputs stay the same. This is the whole point of hiding implementation: it lets libraries improve without forcing every program that depends on them to change too.
This connects directly to procedural abstraction from earlier in the unit. A library is a bundle of abstracted procedures shared across many programs instead of one. It also sets up the idea that modern software is built in layers: your program calls a library, that library might call another, and nobody needs to see all the way down. Understanding libraries and APIs is what makes the next step, reasoning about how programs combine to do complex things, possible.
Key terms
- Library
- A collection of pre-written procedures that programs can call instead of writing that code from scratch.
- API
- Application programming interface; the documented set of procedures a library offers, including their names, inputs, and outputs.
- Abstraction
- Hiding the internal details of how something works while exposing only what it does.
- Procedure
- A named block of code that performs a task and can be called by other code.
Watch it explained
Check yourself
End of topic.