Welcome to the World of Libraries!
In your journey through Computer Science Principles, you have already learned how to write your own procedures (if you need a refresher, check out the chapters on Calling Procedures and Developing Procedures). But here is a secret: professional programmers rarely write every single line of code from scratch! Instead, they use Libraries.
In this chapter, we are going to explore how libraries and APIs help us build complex programs quickly and efficiently by standing on the shoulders of other programmers.
Don't worry if this seems a bit "high-level" right now—by the end of these notes, you will see that using a library is just like using a specialized toolbox.
What is a Library?
A Library is a collection of procedures that can be used to create new programs. Think of it as a pre-packaged set of "skills" that you can add to your code.
The Core Idea: Procedural Abstraction
Libraries are a perfect example of procedural abstraction. This means you only need to know what a procedure does, not how it does it. You don't need to see the hundreds of lines of code inside the library; you just need to know how to call the right procedure to get the result you want.
Why use Libraries?
• Efficiency: Why spend three days writing code to process an image when someone else has already written a library that does it in one second?
• Reliability: Popular libraries are used by thousands of people, meaning most "bugs" (errors) have already been found and fixed.
• Complexity: They allow us to build massive, complicated apps (like Instagram or Google Maps) by combining many smaller, pre-made parts.
Understanding APIs
If a Library is the "toolbox," then the API is the "instruction manual" that tells you how to use the tools. API stands for Application Programming Interface.
An API provides the specifications for how the procedures in a library should be defined and how they should behave. It is essentially the "agreement" between the library and your program.
An Everyday Analogy: The Restaurant Menu
Imagine you are at a restaurant. The kitchen is the Library—it has all the tools and ingredients to make food. You are the Program. You don't go into the kitchen and start cooking; instead, you look at the Menu. The Menu is the API. It tells you exactly what you can order and what you need to tell the waiter (e.g., "I want the burger, medium-well"). As long as you follow the menu's "interface," the kitchen will give you the "output" (your dinner) you expect.
Quick Review: Library vs. API
• Library: The actual collection of code/procedures.
• API: The rules and documentation that tell you how to interact with that code.
Documentation: The Programmer’s Map
To use a library effectively, you must read its documentation. Without documentation, a library is just a box of mystery code! Good documentation for a library procedure will include:
1. The Procedure Name: What is the command called? (e.g., \(calculateTax\))
2. Parameters: What inputs does it need? (e.g., it might need a number for \(price\) and a number for \(rate\))
3. Return Value: What will it give back to you? (e.g., the total cost after tax)
4. Description: A plain-English explanation of what the procedure actually achieves.
Common Mistake to Avoid:
Students often try to "guess" how a procedure works. Always check the documentation! If a procedure expects a list but you give it a single number, the program will likely crash with an error.
Third-Party Libraries
While some libraries come built-in with a programming language, many of the most powerful ones are third-party libraries. These are libraries written by other people or organizations (not the people who made the programming language itself).
When you use a third-party library, you are collaborating with programmers from all over the world! You are using their expertise to make your own program better.
Did you know?
Most modern apps use dozens, or even hundreds, of third-party libraries. There are libraries for everything: drawing 3D graphics, connecting to the Internet, playing sounds, or even calculating the position of the stars!
Key Takeaways for the AP Exam
When you see questions about libraries on the AP Computer Science Principles exam, remember these three main points:
1. Libraries Manage Complexity
By using a library, you are using abstraction. You don't need to understand the underlying code; you only need to understand the interface (the API).
2. Documentation is Essential
You cannot use a library correctly without knowing the names of the procedures and the arguments (the actual values) that need to be passed into the parameters.
3. Reuse and Reliability
Using existing, well-tested libraries is a best practice in programming because it saves time and makes your program more robust (less likely to break).
Check Your Understanding
Imagine you are using a library called WeatherLib. The documentation says:
\(getTemp(city)\): Returns the current temperature in degrees Fahrenheit for the given city name.
If you write the code segment \(currentWeather \leftarrow getTemp("Miami")\), what is happening?
• The Library: WeatherLib.
• The API Call: \(getTemp("Miami")\).
• The Parameter: \(city\).
• The Argument: \("Miami"\).
• The Result: The variable \(currentWeather\) will now hold a number representing the temperature.
You didn't have to build a satellite or a thermometer—you just used a library! That is the power of Big Idea 3.