Lesson 5: Libraries Explore
45 minutes
Overview
Students learn the basics of libraries while building upon the envelope model of a function with a folder to represent a library.
Standards
AAP-3 - Programmers break down problems into smaller and more manageable pieces. By creating procedures and leveraging parameters, programmers generalize processes that can be reused. Procedures allow programmers to draw upon existing code that has already been tested, allowing programmers to write programs more quickly and with more confidence.
AAP-3.D - Select appropriate libraries or existing code segments to use in creating new programs.
- AAP-3.D.1 - A software library contains procedures that may be used in creating new programs.
- AAP-3.D.2 - Existing code segments can come from internal or external sources, such as libraries or previously written code.
- AAP-3.D.3 - The use of libraries simplifies the task of creating complex programs.
- AAP-3.D.4 - Application program interfaces (APIs) are specifications for how the procedures in a library behave and can be used.
- AAP-3.D.5 - Documentation for an API/library is necessary in understanding the behavior(s) provided by the API/library and how to use them.
AP - Algorithms & Programming
- 2-AP-14 - Create procedures with parameters to organize code and make it easier to reuse.
- 3B-AP-16 - Demonstrate code reuse by creating programming solutions using libraries and APIs.
Agenda
Objectives
Students will be able to:
- Clearly write documentation for functions in a library
- Explain the process of preparing a function to be added to a library
- Use appropriate vocabulary to describe libraries
Preparation
- Review the slides and click through all the animations
- Watch the Libraries video which introduces the Libraries tool in App Lab
- Check the "Teacher's Lounge" forum for verified teachers to find additional strategies or resources shared by fellow teachers
- If you are teaching virtually, consider checking our Virtual Lesson Modifications
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the teachers
For the students
- Libraries in App Lab - Video (Download)
Vocabulary
- API - Application Program Interface - specifications for how functions in a library behave and can be used
- Library - a group of functions (procedures) that may be used in creating new programs
Teaching Guide
Warm Up (5 minutes)
Preview Libraries
Discuss: How could you share a function with another person so they could use it in their own program?
Discussion Goal: Guide student answers towards possible answers like these -
- email code to each other
- used a shared document
- copy/paste functions from one tab to another
Remarks
Today we are going to learn a new way to share code.
Activity (35 minutes)
Display: Use the activity slides for this lesson to guide the unplugged activity on Libraries.
Slides | Speaker Notes |
---|---|
Say: Have you ever wanted to share some of your code with a friend so they can use it to add a cool feature in their program? Or maybe you've got a collection of functions in one program that you want to use in another program. Today we are going to explore the answer to this question: How can we easily share functions between programs? | |
![]() Say: These are functions. We recently learned about functions with parameters and return values. ![]() Say: The envelopes have been grouped together in a folder. This represents a library. A library is a collection of functions that can be used in many different programs. | |
Say: Let's take a look at what's inside the library. If we open up the folder, we see the functions and some documentation. A library should have documentation for each of the included functions. The documentation should include:
| |
Say: This detailed type of documentation is also known as Application Program Interface(API). APIs are specifications for how the functions in a library behave and can be used. Discuss: With a partner, review the parts of the documentation. | |
Discuss: What potential problems could come up if I tried to use a function without knowing what it does or how to interact with it? This would be similar to looking at the front of a function envelope and having to guess:
| |
Discuss: My friend wants to use my findSmallest() function in her program. Is this function ready to be shared in a library?![]() Say: Watch out for global variables! If a function accesses or updates a variable elsewhere in your program, that function shouldn't be shared as is. ![]() Do This: With a partner, rewrite the function so it could be shared in a library. ![]() Note: The problem here, is that the function uses a global variable. Students should try to rewrite the function so only local variables and a return are used. The answer is shown later on in the slides. | |
Say: These are the things you should consider before adding a function to a library:
| |
Say: Here's the same function, now written to be shared in a library. How does it look compared to yours? It may look a little different - and that's ok, as long as the function works as expected. Now my function is almost ready to be shared in a library. With a partner, write the API for this function:
![]() | |
Say: A library needs a name. For our libraries in this class, we are going to follow the following rules:
![]() | |
Say: This library can now be shared with others. They can use the functions within their own programs as long as they follow the rules set forth in the documentation.![]() | |
Say: You've seen libraries in action before. The Math library is built into App Lab. Notice the name of the library first, then after a dot, the name of the function and finally the parameters. This is the same formatting you will use to call functions in libraries: Library name, followed by function name, and finally the parameter(s) | |
Do This: Brainstorm with a partner a few functions that might show up in the following libraries: Calculator, DisplayLists, FiterDatabase. Note: Here are some ideas:
|
Remarks
Now let's take a look at how libraries work in App Lab.
Video: Watch the Libraries in App Lab video.
Wrap Up (5 minutes)
Journal: Have students add the following words to their journals: library, API
Remarks
Great work today! Libraries are different than other programming concepts we've explored like variables and conditions. They offer a way to organize and share your code with others.
Assessment: Check For Understanding
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
Question: Evaluate if the following function is a good candidate to be placed in a library. Why or why not?
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.