Parameters, Return, and Libraries - Lesson 6: Libraries Investigate

Overview

In this lesson students work with partners to investigate two apps that use libraries as well as the code used to make a library. Through a series of guided discussions they learn how libraries help programmers simplify and reuse their code.

Goals

Students will be able to:

  • Identify the use of a library within a program
  • Explain the purpose of libraries as a way to simplify programs, allow for code reuse, and enable collaboration.
  • Test the functions in libraries in order to understand their behavior

Purpose

Students get a chance to see how libraries are used in actual apps before they begin building libraries themselves in the subsequent project. The sample apps are written in a clean and simple manner, helping students understand the usefulness of well-documented libraries.

Resources

Getting Started (5 minutes)

Prompt: Today we are going to learn how to use libraries to share code with one another. Usually you do this by writing functions with parameters and return values. Why do you think it's important to use parameters and return values when writing code for other people to use

Discuss: Have students share responses.

Discussion Goal

Some points that might come up:

  • Parameters and return values allow functions to run in predictable ways without impacting other parts of the app unexpectedly
  • Parameters and return values help communicate what the code is supposed to do.
  • Parameters and return values make functions more flexible so that they can be used in a variety of situations.

Activity (35 minutes)

Group: Place students in pairs.

Do this: Get Unit7 Lesson6_App1 from the CSP-Widget repository

Do This: Students navigate to App 1 where they should run the app and try several different inputs.

Discuss: With their partners, students look at the project code and discuss what happens when the button is selected. After a few minutes, pull the class back together and discuss as a whole.

Discussion Goal

When the button is clicked, the screen is updated by calling several functions from a library which return text and images that is displayed.

Do This: Now students should open the library file and look specifically at each StateLibrary function. They should read the documentation for each function before discussing with a partner how these functions work.

Do This: Students read the library code and discuss how the functions actually work. They should consider if they were accurate in their predictions before looking at the library code.

Discuss: What are the benefits of hiding all of the code for filtering the dataset in a library? What information does the user need to know in order to use the library functions?

Discussion Goal

Benefits: The project code is neat and organized.

Information: The end user does not have to know all the inner workings of the functions - they just need to know the documentation for the functions so they can call them correctly and understand what will be returned (if anything).

Do this: Get Unit7 Lesson6_App2 from the CSP-Widget repository

Do This: Students navigate to App 2 where they run the Pigify app and try several inputs.

Do This: Again, students look at the documentation for each function, and discuss how they work.

Test the Functions: Now students practice testing the functions to understand how they work. Here are the steps:

  • Re-read the documentation for each library function
  • Add a output statement to the end of the program and call a function. Put in a reasonable argument in the space for the parameter.
    • For example: output stringsLibrary.FirstLetter("pizza")
  • Hit run to see the output.
  • Now add output statements to test the rest of the functions. Is the output what you would expect? Try several different inputs.

Prompt: Why should we test the functions in the library? What does this help us to know?

Discussion Goal

Goal: Testing the functions allows the end user to understand the behavior of the function. It's helpful for debugging functions, in addition to looking at the code in the library.

For example: If I call a function whose documentation indicates that it will return the first letter of a string, and instead it returns the last number of a string, I know that there is a problem with the library function and not my project code.

Do This: Now students navigate back to the States App and use output to test all of the library functions there.

Prompt: What makes a good library function? How can you make sure that the end users of your library have what they need in order to use your functions?

Discussion Goal

Goal: Hopefully through the process of testing, students will understand the importance of well-documented functions that do what they are expected to do!

A good library function contains everything it needs within the function. Beware of global variables or references to element IDs that might not be present in the end user's projects

Review: Up to this point, students have either created their own algorithms from scratch, or modified existing ones (usually in Investigate Lessons). Now they have another tool to use: combining existing algorithms to make new algorithms. Here, this is accomplished with a library.

Prompt: What are the benefits of using existing algorithms instead of brand new algorithms?

Discussion Goal

Goal: The benefits of using existing algorithms as building blocks for constructing other algorithms include:

We demonstrated the last two bullets in the exercise using output to test functions and isolate errors.

Examples of existing algorithms you may have seen:

  • the maximum or minimum of 2 or more numbers
  • the sum or average of 2 or more numbers
  • an algorithm that determines if an integer can be evenly divided by another integer
  • a robot's path through a maze

Remarks

  • Now let's review Procedural Abstraction. In Lesson 2, we learned about Procedural Abstraction, where shared features of functions (also known as procedures) are extracted in order to generalize use. Procedural Abstraction encourages code reuse and manages complexity, because generalized functions can be used to accomplish many different tasks.
  • Now let's extend that definition. Procedural Abstraction provides a name for a process and allows the procedure to be used only knowing what it does, and not necessarily how it does it.
  • This is how our libraries work!
  • There's a term for using libraries or other forms of organization in a program: Modularity - the subdivision of a computer program into separate subprograms. This is what we are doing when we organize functions into a library and then call them in another program.
  • The end user of the library only needs to know how the functions work, which they can learn through the documentation. This means that the creator of the library can update the functions for any number of reasons, such as making the functions more efficient, without having to notify the end users, as long as the function documentation does not need to be changed. The end users can expect the library to work as intended.

Do This: Review the takeaways. These are focused on the practicalities of creating and using libraries.

Wrap Up (5 minutes)

Synthesis

Prompt: Based on what you saw today, add reasons why someone would argue for the following three statements

  • Libraries help programmers collaborate
  • Libraries help programmers reuse code
  • Libraries help programmers writer simpler programs

Discuss: Have students discuss together before sharing with a class.

Discussion Goal

As you discuss focus conversation on the following points.

  • Libraries help programmer collaborate because they can design code with the idea that other people will use them.
  • Libraries help programmers reuse code because they can be imported into many different programs
  • Libraries help programmers write simpler programs because library code can be used to handle many common (and sometimes complex) behavior and remove it from your core program.

Journal

Students add to their journal the following word and definition: modularity. They may also want to update their definition for procedural abstraction based on earlier conversations and slides.

  • Modularity: the subdivision of a computer program into separate subprograms.

Assessment: Check for Understanding

For Students

Open a word doc or google doc and copy/paste the following question.

Question

Why is it important to use meaningful names for the functions in your library?

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 2-AP-14, 2-AP-17, 3A-AP-18, 3B-AP-16, 3B-AP-23
  • CSP2021: AAP-2.M.1, AAP-2.M.2, AAP-2.M.3, AAP-3.B.1, AAP-3.B.2, AAP-3.B.3, AAP-3.B.4, AAP-3.B.5, AAP-3.B.6, AAP-3.B.7, AAP-3.D.2, AAP-3.D.3

Next Tutorial

In the next tutorial, we will discuss Code.Org Unit 7 Lesson 7, which describes Learn about parameters and return.