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

Overview

Students work with envelopes and paper to model functions with parameters and return values. Students create their own physical function envelope for drawing a house that takes in different parameters, and then build another function to calculate and return the cost of building that house.

Goals

Students will be able to:

  • Use appropriate vocabulary to describe parameters and return values.
  • Remove specifics from a function so that it can be used in a variety of situations

Purpose

In this Explore lesson, students follow a presentation and use manipulatives to understand the basics of creating a function with parameters and return values.

Resources

Supplemental Code.org material

For the students

Preparation

If you want to use physical manipulatives for this lesson, you'll need to prepare the following:

  • Collect various sizes of sticky notes. Small sticky notes are particularly useful for this activity.
  • Two envelopes per group
  • Pencils/pens
  • One baggy for a teacher demonstration (optional)
  • Print out copies of the Activity Guide for students
  • If you are using the slides, review Unit 7 Lesson 1 slides

Getting Started (5 minutes)

Preview Parameterized Functions

  • Up to this point, we've learned the basics of programming: variables, conditionals, functions, lists, loops, and traversals. As you grow in your programming skills, you'll start to notice ways that you can organize your code so that it is easier to work with. That's what this unit is all about!

Prompt: Why would you want to make your code easier to work with or read?

Discussion Goal

Goal: This is a good time to remind students that when we write code, we aren't just writing for a computer - we are also writing for humans who will need to understand our code. Better organization can help others quickly understand our code so they can help debug or build upon the code.

Activity (30 minutes)

Functions with Parameters and Return

Guided Activity: Today's activity introduces students to functions with paramerters and return. As a visual aid, you can use Code.org's presentation slides for Unit 7, Lesson 1: Parameters and Return 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: You and your partner should have

  • Small sticky notes
  • Regular sized sticky notes
  • Pen/Pencil
  • Envelopes

Distribute: large and small sticky notes and envelopes. Hold off on distributing the Activity Guide until later in the activity.

Slide: makeCake tiers flavor

Say: Let's pretend that I want to make a cake. I need to decide how many tiers the cake will have, and what flavor the cake will be. I've decided to make a 3 tiered cake.

Click for animation

makeCake 3 "lemon"

Slide:

Say: If I flip over the envelope, and open it up there's a recipe inside. This recipe is going to tell me how to make the cake I've specified.

Slide:

makeCake 3 "lemon"
repeat tiers times:
    Bake a flavor cake
    Ice the cake.
Assemble tiers layers into one cake.
Put in box.

Say: To do this, I'm going to replace tiers with 3 and flavor with lemon, every time those words show up in the recipe. But notice - this recipe is pretty general. I could use it to make lots of different types and sizes of cake.

Click for animation

makeCake 3 "lemon"
repeat 3 times:
    Bake a "lemon" cake
    Ice the cake.
Assemble 3 layers into one cake.
Put in box.

Do This: Read through the recipe as a class. Make sure everyone understands how it works before moving on.

Slide:

//instructions for baking a cake
makeCake 3 "lemon"
repeat 3 times:
    Bake a "lemon" cake
    Ice the cake.
Assemble 3 layers into one cake.
Put in box.

Say: Ok, let's make our cake. Remember, each time I come across tiers or flavor, I'm going to replace that word with the appropriate value. Once I'm done, I will have baked a 3 layer lemon cake!

Click for animation: There are several steps to click through here. Take it slow so students can see each part.

Slide: makeCake 3 "lemon"

Say: This is a function! It should look familiar, but it has a few extra parts. This function has parameters. Parameters are used as placeholders for values that will be passed through the function.

Click for animation

Say: Those values passed to the parameter are called arguments.

Click for animation

Slide: makeCake tiers flavor

Say: Let's think through how changing the arguments affects the output. Now I want a four layer chocolate cake. With a partner, discuss what my arguments will look like.

Click for animation

Say: I pass through the arguments 4 and chocolate to the parameters tiers and flavor. Then my function is ready. What will my cake look like?

Click for animation

makeCake 4 "chocolate"

Do This: Distribute Activity Guides and direct students to Challenge #1. Students will create their own function envelopes and write a simple function that uses parameters which can be used to draw a house. When students are finished building their function envelopes, a partner tests out the code and draws the house on a spare sticky note.

Slide: Let's look at another function, this time one that calculates the cost of making a cake.

costCake tiers flavor

Say: Here's another function. This function is used to calculate the total cost of making a cake. Take note again of the arguments and parameters.

Click for animation

Say: Let's open the envelope and check out how this function works.

Slide:

//Cost for making a cake
var flavorCost
var toal
If flavor equals "chocolate":
    flavorCost = 5
If flavor equals "lemon":
    flavorCost = 4
If flavor equals "vanilla":
    flavorCost = 3
total = flavorCost multiplied by tiers
return total

Say: There are a few new things here. First, we've created two local variables. Remember, the local variables are contained within the function and can't be updated or accessed outside of the function. We learned about this when we discussed variable scope.

Click for animation

From Slide: Create two new local variables, flavorCost and total.

In this function, a decision (in this case, how much to charge per flavor) is made based on the argument passed through the parameter.

Click for animation

From Slide: Determine the value of flavorCost based on the argument passed through the flavor paramter.

Finally, the total is returned, but returned where? What does that mean?

Click for animation

From Slide: Calculate total using flavorCost and the argument passed through the tiers paramter.

Slide: After running this what does flavorCost equal? What does total equal?

Say: Before we talk more about that return, let's calculate the total cost of our original cake.

Do This: With a partner, determine what flavorCost and total equal after the function is run.

Slide: 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.

Say: Now let's think about the return. If the program hits a return at any point in running a function, it will stop and a value will be "returned". Where is it returned? To the place in the program where the function was called.

Slide: We've called the cakeCost function. It has returned the value 12.

But what happens to that value?

How is it stored?

Say: So to get back to our example, the value 12 is returned, but where is it returned to? Where is it stored?

Do This: Take a moment to brainstorm with a partner.

Slide: Let's return to variable baggies!

A function return value can be stored in a variable.

Say: Let's return to variable baggies! A function return value can be stored in a variable. Notice how the envelope is placed inside of a baggie. That means that the function would be evaluated and whatever is returned would be stored in the variable cakeCalculator

Click for animation

Slide: var cakeCalculator = cakeCost(3, "lemon");

After the expression is evaluated, cakeCalculator stores the value 12.

Say: Here's how this looks in JavaScript. It's ok if it's a little confusing. For now, notice how the statement is set up. A variable is set up with the name cakeCalculator. This gets the value that is returned from the function cakeCost which has two arguments 3 and lemon passed through the parameters. After the function is evaluated, cakeCalculator now stores the value 12.

Say: In Quorum, the code looks similar. The biggest difference is that the variable must be of the correct type. Return types will be covered later.

integer cakeCalculator = CakeCost(3, "lemon")

Slide: console.log("Cake cost: " + cakeCost(3, "lemon"));

Say: We can also print to the console like so.

Say: In Quorum you can use output

output "Cake cost: " + CakeCost(3, "lemon")

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

Do This: Direct students to complete Challenge #2 in their Activity Guides. Students create a new envelope function which calculates the cost of the house from Challenge #1. After students are finished, they share the envelopes with a partner who tests the code.

  • 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

Wrap Up (10 minutes)

Takeaways: Review with the class

Journal

Students should add the following words and definitions to their journals: parameter, argument, return.

  • 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.

Assessment: Check for Understanding

For Students

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

Question

Read through the code and choose the correct answer.

output Numbers(3,2)

action Numbers(number num1, number num2) returns number
    number answer
    answer = num1 + num2
    answer = answer + num1
    return answer * num2
end
  • 15
  • 16
  • 21
  • 18

Standards Alignment

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

Next Tutorial

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