Unit 7: Parameters, Return, and Libraries
Unit 7: Parameters, Return, and Libraries
Prompt:
“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
“lemon”
makeCake 3
tiers “lemon”
flavor
makeCake 4
tiers flavor
“chocolate”
Unit 7 Lesson 1 - Activity
“lemon”
costCake 3
tiers “lemon”
flavor
Parameters
Unit 7 Lesson 1 - Activity
tiers “lemon”
flavor
cakeCost 3
Unit 7 Lesson 1 - Activity
???????
Unit 7 Lesson 1 - Activity
3 “lemon”
How is it stored?
Unit 7 Lesson 1 - Activity
rem
“lfla voon”
Let’s return to
variable baggies!
cakeCalculator
Unit 7 Lesson 1 - Activity
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.
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
● 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?
Review
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
MOD 3%2 1
MOD is the remainder that is left after a number is divided by another number
Unit 7 Lesson 2 - Activity
17 % 5
3 remainder 2
5 17
-15
2
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:
Takeaways:
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
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?
Prompt:
Prompt:
Do This:
● Navigate to Lesson 4,
Level 2 on Code Studio
Unit 5 Lesson 8 - Activity
Prompt:
Prompt:
Prompt:
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.
CakeBaker
cakeCost tiers flavor
APIs are specifications for how the functions in a library behave and can be used.
Unit 7 Lesson 5 - Activity
Discuss:
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.
☑
1. Check for any use of a global variable
within the function. If there is, rework
the function using local variables and a
return.
Ba ke r
Cake
Unit 7 Lesson 5 - Activity
Do This: Brainstorm with a partner a few functions that might show up in the
following libraries:
Vocabulary
Prompt:
Do This:
● Navigate to Level 2
● Run the app
● Try several different inputs
Unit 7 Lesson 6 - Activity
Discuss:
Do This:
Do This:
● Click "Manage Libraries"
Prompt:
What are the benefits of hiding all of the code for
filtering the dataset in a library?
Do This:
● Navigate to Level 3
● Run the app
● Try several different inputs
Unit 7 Lesson 6 - Activity
Do This:
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:
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
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.
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
Vocabulary:
Prompt:
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"?
Prompt:
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
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
Step 5 - Feedback -
Export Your Library
1. In Lesson 9, Level 2 click Share → Show
Advanced Options → Share as Library
3. Hit Publish
Unit 7 Lesson 9 - Activity
Step 5 - Feedback -
Import a Library
1. Go to the next level, Lesson 9 Level 3
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
● 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
Unit Assessment