0% found this document useful (0 votes)
448 views146 pages

Unit 7: Parameters, Return, and Libraries

Okay, here are the step-by-step workings: 17 / 5 = 3 with a remainder of 2 So, 17 % 5 = 2 The % operator returns the remainder after a number is divided by another number. In this case, when we divide 17 by 5, the remainder is 2.

Uploaded by

francis zheng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
448 views146 pages

Unit 7: Parameters, Return, and Libraries

Okay, here are the step-by-step workings: 17 / 5 = 3 with a remainder of 2 So, 17 % 5 = 2 The % operator returns the remainder after a number is divided by another number. In this case, when we divide 17 by 5, the remainder is 2.

Uploaded by

francis zheng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 146

Unit 7: Parameters, Return, and Libraries

Lesson 1: Parameters and Return Explore Lesson 7: Libraries Practice


Lesson 2: Parameters and Return Investigate Lesson 8: Project - Libraries Part 1
Lesson 3: Parameters and Return Practice Lesson 9: Project - Libraries Part 2
Lesson 4: Parameters and Return Make Lesson 10: Project - Libraries Part 3
Lesson 5: Libraries Explore Lesson 11: Assessment Day
Lesson 6: Libraries Investigate
Unit 7 - Lesson 1
Parameters and Return Explore
Unit 7 Lesson 1 - Warm Up

Prompt:

Why would you want to make your


code easier to work with or read?
Unit 7 Lesson 1 - Activity

You and your partner should have


Small sticky notes
Regular sized sticky notes
Pen / Pencil
Envelopes
Unit 7 Lesson 1 - Activity

“lemon”

makeCake 3
tiers “lemon”
flavor
Unit 7 Lesson 1 - Activity

tiers “lemon”
flavor
makeCake 3
Unit 7 Lesson 1 - Activity

tiers “lemon”
flavor
makeCake 3

3 “lemon”

“lemon”

3
Unit 7 Lesson 1 - Activity

3 “lemon”

“lemon”

3
Unit 7 Lesson 1 - Activity

Argument - the value passed to


the parameter

“lemon”

makeCake 3
tiers “lemon”
flavor

Parameter - a variable in a function definition.


Used as a placeholder for values that will be
passed through the function.
Unit 7 Lesson 1 - Activity

What if I wanted a four layer


chocolate cake? What 4 “chocolate”

would that look like?

makeCake 4
tiers flavor
“chocolate”
Unit 7 Lesson 1 - Activity

Do This: Time to create your own function


with parameters!

● Complete Challenge #1 in your Activity


Guide
Unit 7 Lesson 1 - Activity

Let’s look at another function, this


Arguments
time one that calculates the cost
of making a cake.
3

“lemon”

costCake 3
tiers “lemon”
flavor

Parameters
Unit 7 Lesson 1 - Activity

tiers “lemon”
flavor
cakeCost 3
Unit 7 Lesson 1 - Activity

Create two new local variables,


flavorCost and total.

Determine the value of flavorCost based


on the argument passed through the flavor
parameter

Calculate total using flavorCost and the


argument passed through the tiers
parameter

???????
Unit 7 Lesson 1 - Activity

3 “lemon”

After running this, what


does flavorCost equal? 4
What does total equal? 12
Unit 7 Lesson 1 - Activity

3 “lemon” What does it mean to


return total?

A return does two things:


● It stops the flow of the
function. If a return is inside
of a conditional, if that
condition is met the function
ends there.
● It returns a value to the
place where the function
was called.
Unit 7 Lesson 1 - Activity

We’ve called the


cakeCost function. It
tiers
3 flavor
“lemon”
cakeCost has returned the
value 12.

But what happens to


12 that value?

How is it stored?
Unit 7 Lesson 1 - Activity

rem
“lfla voon”
Let’s return to
variable baggies!

ttie11rs A function return


cakeCos

12 value can be stored


in a variable.

cakeCalculator
Unit 7 Lesson 1 - Activity

Here’s how this looks in Javascript:

var cakeCalculator = cakeCost(3, “lemon”);

After the expression is evaluated,


cakeCaculator stores the value 12.
Unit 7 Lesson 1 - Activity

We can also print to the console like so:

console.log(“Cake cost: ” + cakeCost(3, “lemon”));

Console

Cake cost: 12
Unit 7 Lesson 1 - Activity

Do This: Create a cost calculator function for building the house you
created a function for earlier.

● Complete Challenge #2 on your Activity Guide


Unit 7 Lesson 1 - Wrap Up

Takeaways

● Functions with parameters and return values help us simplify our code
● Functions can only return one value at a time
● A function can have:
○ No parameters and no return values
○ Parameters, but no return values
○ Return values, but no parameters
○ Parameters and return values
Unit 7 Lesson 1 - Wrap Up

Vocabulary

● Parameter - a variable in a function definition. Used as a placeholder for


values that will be passed through the function.

● Argument - the value passed to the parameter

● Return - used to return the flow of control to the point where the
procedure (also known as a function) was called and to return the value
of expression.
Unit 7 - Lesson 2
Parameters and Return Investigate
Unit 7 Lesson 2 - Warm Up

Prompt:
Are clean and organized programs
more useful for computers or people?

Why?

Try to give examples from programs


you've written or seen in this class.
Unit 7 Lesson 2 - Activity

Review

● Functions with parameters and return values help us


simplify our code
● Functions can only return one value at a time
● A function can have:
○ No parameters and no return values
○ Parameters, but no return values
○ Return values, but no parameters
○ Parameters and return values
Unit 7 Lesson 2 - Activity

Do This:
● Find a partner
● Navigate to Level 2
● Read the program with your
partner
● Respond to all the questions for
your app
● Be ready to share your responses
and what you learned with
the class.
Unit 7 Lesson 2 - Activity

Discuss as a class:
● How does calculate(symbol)
work?
● What is the parameter?
● What is the argument?
● What is returned?
Unit 7 Lesson 2 - Activity

Do This:
● Look at lines 30-36. Discuss with a
partner how the MOD operator %
works.
Unit 7 Lesson 2 - Activity

MOD

Add 3+2 5

Subtract 3-2 1

Multiply 3*2 6

Divide 3/2 1.5

MOD 3%2 1

MOD is the remainder that is left after a number is divided by another number
Unit 7 Lesson 2 - Activity

Let's Practice the answer

17 % 5
3 remainder 2
5 17
-15
2
Unit 7 Lesson 2 - Activity

When is this useful?


● A common usage is to determine if a number is even or odd.
If you divide any number by two and there is no remainder,
the number is even!
● You can use MOD to determine if a number is divisible by
another number.
Unit 7 Lesson 2 - Activity

Do This:
● With your partner:
● Navigate to Level 3
● Read lines 1-14
● What is happening here?
● Be prepared to discuss as a class.
Unit 7 Lesson 2 - Activity

Do This:
● Now, read the function on lines 15-34 carefully.
After you are done, explain to your partner
how the function works, what parameters it
takes and what is returned.
● Extra time? Complete the modify.
Unit 7 Lesson 2 - Activity

Discuss as a class:

How does the Word Game


Helper work?
Unit 7 Lesson 2 - Wrap Up

Takeaways:

Extracting shared features to generalize functionality is known as procedural


abstraction.

Using parameters allows the functions (also called procedures) to be generalized.

Using procedural abstraction helps improve code readability.

Procedural abstraction manages complexity by allowing for code reuse.

● For example: the function move(id, direction) could be used to move


an element in any direction, rather than writing separate functions for each
direction.
Unit 7 - Lesson 3
Parameters and Return Practice
Unit 7 Lesson 3 - Warm Up

Prompt: What is one reason why


parameters and return values are useful?

What is one way you think programming


with parameters and return values may
make programming or debugging more
challenging?
Unit 7 Lesson 3 - Activity

Debugging: the process of finding and fixing problems in code

Describe Hunt
The Problem For Bugs
What do you expect it to do? Are there warnings or errors?
What does it actually do? What did you change most recently?
Does it always happen? Explain your code to someone else
Look for code related to the problem

Try Document
Solutions As You Go
Make a small change What have you learned?
What strategies did you use?
What questions do you have?
Unit 7 Lesson 3 - Activity

Debugging Functions with Parameters and Return

Use the speed


slider to slow down
code so you can
watch how
functions are being
called. This will
highlight lines of
code in yellow as
the program runs.

Use console.log to call functions


with parameters and see how calling
them with different values returns
different values
Unit 7 Lesson 3 - Activity

Parameters and Return Practice

Do This:
● Navigate to Lesson 3,
Level 2 on Code Studio
Unit 7 Lesson 3 - Wrap Up

Prompt:
What aspects of working with
parameters and return values do you
feel like clicked today?

What do you still feel like you have


trouble with?
Unit 7 Lesson 4 - Wrap Up

Prompt:

How could using parameters and


return help you write programs
collaboratively?
Unit 7 - Lesson 4
Parameters and Return Make
Unit 5 Lesson 4 - Warm Up

Prompt:

How do parameters and return change


the way you write programs?
Unit 5 Lesson 4 - Warm Up

This Make Project is different

You'll get most of the code but three functions are


incomplete. They always return the same value.

You'll need to rewrite these three functions using the


comments provided
Unit 5 Lesson 4 - Activity

Parameters and Return Make:


Rock Paper Scissors App

Do This:
● Navigate to Lesson 4,
Level 2 on Code Studio
Unit 5 Lesson 8 - Activity

Prompt:

● What does each button do


● How does the screen get updated after clicking
each button
Unit 7 Lesson 4 - Activity

This Make project is different

You'll have most of the code but three functions are


incomplete. They always return the same value.

You'll need to rewrite these three functions using the


comments provided. No other edits in the program are
necessary.
Unit 5 Lesson 4 - Activity

Do This: Make the Rock Paper Scissors App!


Use the activity guide to plan out your code.

Don't forget you're only working inside of the


three functions at the bottom of the program. Use
programming patterns to help and test your code
as you go.

Step 3 includes steps you can follow to build the


app, or you can use your own process.
Unit 5 Lesson 4 - Activity

Don't forget: check the rubric on the last


level before hitting submit
Unit 5 Lesson 4 - Wrap Up

Prompt:

How could using parameters and


return help you write programs
collaboratively?
Unit 7 - Lesson 5
Libraries Explore
Unit 7 Lesson 5 - Warm Up

Prompt:

How could you share a function with another


person so they could use it in their own
program?
Unit 7 Lesson 5 - Activity

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 own program?

Or maybe you’ve got a collection of functions in one program that you want to
use in another program.

How can we easily share


functions between programs?
Unit 7 Lesson 5 - Activity

CakeBaker
cakeCost tiers flavor

makeCake tiers flavor


averageCake cakeList

This is a library - a collection of functions that


can be used in many different programs.
Unit 7 Lesson 5 - Activity

A library should have


makeCake - creates a cake
● tiers {number} - the layers of the cake documentation for the
● flavor {string} - the flavor of the cake included functions:
cakeCost - calculates the cost of making a cake
● tiers {number} - the layers of the cake
● flavor {string} - the flavor of the cake ● how each function
● return {number} - the cost of making the cake works
avgCake - calculates the most common cake
cakeList {List} - a list of strings of cake names

● return {string} - the most common cake
● a complete list of
the parameters

● what (if anything)


flav will be returned
cakeCost tiers
or
averageCake cakeList

makeCake tiers flavor


Unit 7 Lesson 5 - Activity

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
● return {number} - the cost of making the cake

avgCake - calculates the most common cake


● cakeList {List} - a list of strings of cake names
● return {string} - the most common cake

This detailed type of documentation is also known as an:

Application Program Interface(API)

APIs are specifications for how the functions in a library behave and can be used.
Unit 7 Lesson 5 - Activity

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:

averageCake cakeList
● what the function does
● what data type the parameters need
● what is returned
Unit 7 Lesson 5 - Activity

Discuss:
1 var smallest;
2 My friend wants to use my
3 findSmallest(34, 99); findSmallest() function in her
4
5 function findSmallest(num1, num2){ program. Is this function ready to
6 if(num1 < num2){ be shared in a library?
7 smallest = num1;
8 } else {
9 smallest = num2; Why or why not?
10 }
11 } Watch out for global variables! If a
12 function accesses or updates a variable
elsewhere in your program, that
function shouldn't be shared as is.

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

Hint: What about using a return?


Unit 7 Lesson 5 - Activity

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.
Unit 7 Lesson 5 - Activity

function findSmallest(num1, num2){


if(num1 < num2){ Now my function is almost ready
return num1; to be shared in a library.
} else {
return num2;
} With a partner, write the API for
} this function:

● how the function works


findSmallest: Given two numbers, finds the smallest
● num1 {number} - first number ● all the parameters, their data
● num2 {number} - second number types, and a short description
● return {number} - the smaller of the two numbers
of each

● what (if anything) will be


returned
Unit 7 Lesson 5 - Activity

A library needs a name.

Follow these rules:


● No spaces
● Capitalize the first letter

Ba ke r
Cake
Unit 7 Lesson 5 - Activity

This library can now be


shared with others.

They can use the functions


within their own program as
long as they follow the rules

Ba ke r set forth in the


Cake documentation.
Unit 7 Lesson 5 - Activity

You’ve seen libraries in action before...

The Math library is built


into App Lab.

Name of the library Name of the function Parameters


Unit 7 Lesson 5 - Activity

Do This: Brainstorm with a partner a few functions that might show up in the
following libraries:

Calculator DisplayLists FilterDatabase


Unit 7 Lesson 5 - Activity

Libraries in App Lab


Unit 7 Lesson 5 - Wrap Up

Vocabulary

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
Unit 7 - Lesson 6
Libraries Investigate
Unit 7 Lesson 6 - Warm Up

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?
Unit 7 Lesson 6 - Activity

Do This:

● Navigate to Level 2
● Run the app
● Try several different inputs
Unit 7 Lesson 6 - Activity

Discuss:

● With a partner, look at the


project code.
● Discuss what happens when
the button is clicked.
Unit 7 Lesson 6 - Activity

Do This:

● Open the functions drawer


● Look at each of the
StateLibrary functions.
Mouseover for the
documentation.
● Discuss with a partner how
you think these functions
work.
Unit 7 Lesson 6 - Activity

Do This:
● Click "Manage Libraries"

● Click "view code" for the State


Library

● With your partner, read through the


library and discuss how the
functions work. Were you accurate
in your predictions?
Unit 7 Lesson 6 - Activity

Prompt:
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?
Unit 7 Lesson 6 - Activity

Do This:

● Navigate to Level 3
● Run the app
● Try several different inputs
Unit 7 Lesson 6 - Activity

Do This:

● Open the functions drawer


● Look at each of the
StringsLibrary functions.
Mouseover for the
documentation.
● Discuss with a partner how
you think these functions
work.
Unit 7 Lesson 6 - Activity

Test the functions:

● Re-read the documentation for each library function


● Add a console.log() statement to the end of the program and call a
function. Put in a reasonable argument in the space for the parameter.
○ For example:
console.log(StringsLibrary.firstLetter("pizza"));
● Hit run to see the output.
● Now add console.log() statements to test the rest of the functions.
Is the output what you would expect? Try several different inputs.
Unit 7 Lesson 6 - Activity

Prompt:
Why should we test the functions in the library?
What does this help us to know?
Unit 7 Lesson 6 - Activity

Do This:
● Navigate back to the States App
● Add console.log statements for each of the
functions and test them out. Is the output as
expected?
Unit 7 Lesson 6 - Activity

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?
Unit 7 Lesson 6 - Activity

Let's quickly review Algorithms:

Up to this point, most of the algorithms you've used you created yourself, or you
modified existing code.

Do This: Look at the functions in Levels 2 & 3 that call the library functions.

Notice here how we can build new functions by combining the existing library
functions - essentially we are creating new algorithms by using existing algorithms
(library functions).
Unit 7 Lesson 6 - Activity

Prompt: What are the benefits of using existing algorithms


instead of brand new algorithms?

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
Unit 7 Lesson 6 - Activity

Let's quickly review Procedural Abstraction:

Procedural Abstraction provides a name for a process and allows the procedure
(function) 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.


Unit 7 Lesson 6 - Activity

Takeaways:
● Creating a library:
○ Build functions
○ Add documentation
○ Share as a Library
● Using a library:
○ Click "Manage Libraries"
○ Either choose a classmate's library, or paste in a library code
○ Call the functions by writing the library name, a dot, the name of the function,
and including any arguments for the parameters
● Testing a library:
○ Use console.log as the end user to test functions in a library
○ Check that the output is what you would expect
○ Read the library code if something does not work correctly, and contact the
library owner if something needs to be changed.
Unit 7 Lesson 6 - Wrap Up

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

● Libraries help programmers collaborate because...


● Libraries help programmers reuse code because...
● Libraries help programmers writer simpler programs
because...
Unit 7 Lesson 6 - Wrap Up

Vocabulary:

Modularity: the subdivision of a computer program into


separate subprograms
Unit 7 - Lesson 7
Libraries Practice
Unit 7 Lesson 7 - Warm Up

Prompt:

How does using a library allow you to


think about programming at
"a higher level"?
Unit 7 Lesson 7 - Activity

Debugging: the process of finding and fixing problems in code

Describe Hunt
The Problem For Bugs
What do you expect it to do? Are there warnings or errors?
What does it actually do? What did you change most recently?
Does it always happen? Explain your code to someone else
Look for code related to the problem

Try Document
Solutions As You Go
Make a small change What have you learned?
What strategies did you use?
What questions do you have?
Unit 7 Lesson 7 - Activity

Testing Functions

Use console.log to write tests of the Check the results in the console to
function. Try different values to make make sure that the functions pass the
sure your function works in many test.
cases.
Unit 7 Lesson 7 - Activity

Libraries Practice

Do This:
● Navigate to Lesson 7,
Level 2 on Code Studio
Unit 7 Lesson 7 - Wrap Up

Prompt:
How do libraries let you write
programs at a "higher level"?

Why is testing important when building


and sharing libraries?
Unit 7 - Lesson 8
Project - Make a Library Part 1
Unit 7 Lesson 8 - Warm Up

Prompt:

Think back over all the different apps


you've built this year. What blocks do
you wish already came with App Lab to
help you build those apps?
Unit 7 Lesson 8 - Activity

Project - Make a Library

● Read the project description


● Review what you'll submit, steps of
the project, and rubric
Unit 7 Lesson 8 - Activity

Step 1 - Brainstorm
Brainstorm a theme for your library.
● What kind of blocks do you want to
add to App Lab?
● What situations do you want to make
easier?
Unit 7 Lesson 8 - Activity

Step 2 - Design
● Choose 2 or more functions you'd
like to build.
● At least one needs a parameter,
return, loop, and if-statement
● Fill in step 2 of the Project Guide
Unit 7 Lesson 8 - Activity

Step 3 - Build

Use the rest of your time today to build


out the functions you designed.
Unit 7 - Lesson 9
Project - Make a Library Part 2
Unit 7 Lesson 9 - Warm Up

Two ways to test your library

1. Write tests!
2. Have a classmate try it out
Unit 7 Lesson 9 - Activity

Tests refresher

Use console.log to write tests of the Check the results in the console to
function. Try different values to make make sure that the functions pass the
sure your function works in many test.
cases.
Unit 7 Lesson 9 - Activity

Step 4 - Test
● As your program today, add tests to
your functions to make sure they're
working as you expect
● Keep writing your library
Unit 7 Lesson 9 - Activity

Before moving to Step 5


● Once you’ve finished testing your
functions, comment out your tests
Unit 7 Lesson 9 - Activity

Step 5 - Feedback -
Export Your Library
1. In Lesson 9, Level 2 click Share → Show
Advanced Options → Share as Library

2. Choose the functions you'd like to


export. If you need to edit the
comments before your functions do so.

3. Hit Publish
Unit 7 Lesson 9 - Activity

Step 5 - Feedback -
Import a Library
1. Go to the next level, Lesson 9 Level 3

2. Click the gear and then Manage Libraries

3. Find your partner's library and import it

4. Start testing the different functions they


shared with you. They'll be in the
"Functions" drawer
Unit 7 Lesson 9 - Activity

Step 5 - Feedback - Give


Feedback
● On your classmate's project guide give them feedback
about their library.

● Hover over blocks to read their documentation

● You can view all the library code by clicking "View


Code" from the "Manage Libraries" window
Unit 7 Lesson 9 - Activity

Step 6 - Improve
● If you have time continue working on improving your library
based on your feedback and testing from today
Unit 7 - Lesson 10
Project - Make a Library Part 3
Unit 7 Lesson 10 - Activity

Step 7 - Acknowledge Collaborators


● Fill in the table acknowledging the source of any code your partner
wrote or that you got from another source
Unit 7 Lesson 10 - Activity

Step 8 - Free Response


● Complete the free response questions about one of the functions
in your project

● If you have more time keep working on your library and check the
Scoring Guidelines to make sure you're ready to submit.
Unit 7 Lesson 11 - Wrap Up

Submit

● Turn in your project guide


● Hit "Submit" on Lesson 10 Level 2
Unit 7 - Lesson 11
Assessment Day
Unit 7 Lesson 12 - Activity

Unit Assessment

You might also like