Unit 3: Programming · Procedural abstraction and libraries

Article

Libraries and APIs

6 min read

You did not write the code that plays a video in your browser, or the code that tells you tomorrow's weather. Other programmers wrote it and shared it. When you use their work in your own program, you are using a library.

Software libraries

A software library is a collection of ready-made procedures that you can use in your own programs. You call a library procedure in the same way as your own procedures: its name, brackets and arguments. You do not have to write it, and you do not have to know how it works inside. That is procedural abstraction again, but this time somebody else built the procedure.

Python has many libraries. Two of them are math and random. In Python you write import to say which library you want to use. Run this:

Runs in your browser

The output is 7.0, 7 and 8. We can describe what each procedure does without looking at its code:

  • math.sqrt(x) gives the square root of x.
  • math.floor(x) rounds x down to a whole number.
  • math.ceil(x) rounds x up to a whole number.

Now the random library. Run this several times:

Runs in your browser

You get a different number each time, from 1 to 6. The procedure random.randint(a, b) gives a random whole number from a to b, including both ends. The AP exam has a procedure that does the same job, and you have already met it:

AP pseudocode
DISPLAY(RANDOM(1, 6))

Check your understanding

What is a software library?

APIs and documentation

How do you know that randint needs two arguments, or that it includes both ends? You read its description. The API (application programming interface) of a library is the specification of its procedures: their names, what arguments each needs, what each does and what each gives back. The written description is the documentation.

Three layers when you use a library

  1. Your program

    Calls sqrt(25) and uses the number that comes back.

  2. The API and its documentation

    sqrt(x) gives back the square root of x.

  3. The library's code

    How the square root is worked out. You do not need to read it.

You write to the API. The code behind it can change without breaking your program.

Good documentation answers four questions about every procedure: What is it called? What does it need (its parameters)? What does it do? What does it give back (its return value)? Here is documentation for a made-up library called ShopLib:

ProcedureWhat it does
price(item)Returns the price in dollars of item. item is a piece of text. For example, price("book") returns 50.
discount(amount, percent)Returns amount after taking off percent % of it. For example, discount(200, 10) returns 180.

Using only this table, you can write programs with ShopLib. You never see the code inside.

Check your understanding

The documentation of a library says: discount(amount, percent) returns amount after taking off percent % of it. What does discount(80, 25) return?

Why use libraries?

  • They save time. You do not write and test everything yourself.
  • They are reliable. Many people have already used and checked them.
  • They let you do hard things, such as drawing graphs or playing sound, with one call.
  • They need documentation. Without it, you would have to guess what a procedure does and which arguments it needs.

Worked example

Use the ShopLib documentation above, and suppose price("pen") returns 20. What does this program display?

AP pseudocode
cost ← price("book")
DISPLAY(discount(cost, 10))
DISPLAY(discount(price("pen"), 50))
  1. price("book") returns 50, as the documentation says. So cost is 50.
  2. discount(cost, 10) takes 10% off 50, which leaves 45. The program displays 45.
  3. price("pen") returns 20. Then discount(20, 50) takes half off, which leaves 10. The program displays 10.

The output is 45 and 10. You found it by reading the documentation, not the code.

Common mistakes

  • Trying to read the library's code to use it. The documentation tells you everything you need.
  • Guessing the order of the arguments. In discount(amount, percent), the amount comes first. Check the documentation.
  • Mixing up API and library. The library is the code. The API is the description of how to use it.
  • Forgetting import in Python. Without it, Python does not know the name math or random.
  • Expecting random procedures to give the same answer each time. They do not.

On the exam

Questions often show a short piece of documentation and ask what a call returns, or which call gives a certain result. Read each procedure's description carefully, and trust it: do not worry how it works. Other questions ask why a programmer would use a library, or why documentation is needed.

Key words

Software library: a collection of procedures that programmers can use in their own programs.

API (application programming interface): the specification of how the procedures in a library behave and how to use them.

Documentation: the written description of an API, which says what each procedure needs, does and returns.

Import: the Python word for bringing a library into a program.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.