Parameters, Return, and Libraries - Lesson 5: Libraries Explore

Overview

Students learn the basics of libraries while building upon the envelope model of a function with a folder to represent a library.

Goals

Students will be able to:

  • Use appropriate vocabulary to describe libraries
  • Explain the process of preparing a function to be added to a library
  • Clearly write documentation for functions in a library

Purpose

The mental model of a folder containing a group of functions is used to introduce the concept of a library. Throughout this lesson student explore the requirements for working with libraries and documentation.

Resources

Getting Started (5 minutes)

Preview Libraries

Prompt: How could you share a function with another person so they could use it in their own program?

Discussion Goal

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)

Guided Activity: Today's activity introduces students to libraries. As a visual aid, you can use Code.org's presentation slides for Unit 7, Lesson 5: Libraries Explore. These slides include animations. The notes below describe when to move to the next slide or click through an animation -- if you aren't using the slides, you can ignore these prompts.

Guided Activity

Follow this lesson with the supplemental slides.

Slide: How can we easily share functions between programs?

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?

Slide: Take the following functions:

makeCake tiers flavor
cakeCost tiers flavor
averageCake cakeList

And imagine them in an folder named CakeBaker

This is a library - a collection of functions that can be used in many different programs.

Click for animation: Stop once all three envelopes are on the screen.

Say: These are functions. We recently learned about functions with parameters and return values.

Click for animation: Click until the library definition appears on the screen.

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.

Slide:

makeCake - creates a cake

  • tiers {number} - the layers of the cake
  • flavor {string} - the flavor of the cake

cakeCost - calculates the cost of making a cake

  • tiers {number} - the layers of the cake
  • flavor {string} - the flavor of the cake

avgCake - calculates the most common cake

  • tiers {number} - the layers of the cake
  • flavor {string} - the flavor of the cake

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:

  • how each function works
  • a complete list of the parameters
  • what (if anything) will be returned

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.

Slide: averageCake cakeList

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:

  • what the function does
  • what data type the parameters need
  • what is returned

Note: If students are struggling to come up with ideas, give a few hints:

  • what if there's an error when you run the code which calls this function?
  • what happens to a return if you don't store it in a variable or print it to the console?

Slide:

var smallest;

findSmallest(34, 99);

function findSamllest(num1, num2){
    if(num1 < num2){
        smallest = num1;
    } else {
        smallest = num2;
    }
}

Discuss: My friend wants to use my findSmallest() function in her program. Is this function ready to be shared in a library?

Click for animation

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.

Click for animation

Do This: With a partner, rewrite the function so it could be shared in a library.

Click for animation

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.

Slide: Before adding a function to a library:

  1. Check for any use of a global variable within the function. If there is, rework the function using local variables and a return.
  2. Check if another function is called in this function. If so, both functions should be included in the library.
  3. Write the documentation for the function.

Say: These are the things you should consider before adding a function to a library

Slide:

function findSamllest(num1, num2){
    if(num1 < num2){
        return num1;
    } else {
        return num2;
    }
}

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:

  • how the function works
  • all the parameters, their data types, and a short description of each
  • what (if anything) will be returned

Click for animation: When the class is ready, click through to see a possible answer.

findSmallest: Given two numbers, finds the smallest

  • num1 (number) - first number
  • num2 (number) - second number
  • return (number) - the smaller of the two numbers

Slide: CakeBaker

Say: A library needs a name. For our libraries in this class, we are going to follow the following rules:

  • No spaces
  • Capitalize the first letter

Click for animation

Slide: CakeBaker

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.

Click for animation

Slide: Math.round(5, 10)

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)

Slide: Calculator, DisplayLists, FilterDatabase

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:

  • Calculator: functions similar to what you would find in a common calculator
  • DisplayLists: functions that help you display a list on a screen in different ways, such as one list item per line or divided with bullet points
  • FilterDatabase: functions that allow you to filter a database for different things, like all the items that start with the letter "a"

Remarks

  • Let's take a look at how libraries work in Quorum.

For more information on loading and interacting with libraries in Quorum you can use the Use Statements tutorial to learn more about the use statement and interacting with the Quorum Standard Library. If you have been programming in Quorum you might have already seen these use statements. The standard library is essential in getting apps to work so you have been using libraries this whole time and you might not have even known it!

As you will see in the Apps associated with this unit, in Quorum, when you want to make your own library the library functions will be placed in there own file within the project. Quorum also already has a large collection of functions and classes available to you and they are collectively known as the Quorum Standard Library. The standard library is included with Quorum Studio so those files won't be directly included in the project but they are available to you using a use statement. Here is an example of using the Math library from the standard library:

use Libraries.Compute.Math
class Main
     action Main
          Math math
          output math:SquareRoot(6)
     end
end

From the code sample you can see that the Math library was made available to us by the line use Libraries.Compute.Math. To use the functions in the Math library you need a Math variable which is why we have Math math. With the math variable you can now use it call the functions it has like the SquareRoot function.

Wrap Up (5 minutes)

Journal: Have students add the following words to their journals: Library, API

  • Library: a group of functions (procedures) that may be used in creating new programs
  • API: Application Program Interface - specifications for how functions in a library behave and can be used

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

For Students

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

Question

Evaluate if the following function is a good candidate to be placed in a library. Why or why not?

function updateScore(player, points) {
    if(player == "player1"){
        player1Points = player1Points + points;
    } else {
        player2Points = player2Points + points;
    }
}

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 2-AP-14, 3B-AP-16
  • CSP2021: AAP-3.D.1, AAP-3.D.2, AAP-3.D.3, AAP-3.D.4, AAP-3.D.5

Next Tutorial

In the next tutorial, we will discuss Code.Org Unit 7 Lesson 6, which describes Learn about libraries.