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.

Purpose

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

Agenda

Lesson Modifications

Warm Up (5 mins)

Activity (30 mins)

Wrap Up (10 mins)

View on Code Studio

Objectives

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

Preparation

  • 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
  • Review the Unit 7, Lesson 1 slides prior to the class

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Teachers

For the Students

Teaching Guide

Lesson Modifications

Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.

Warm Up (5 mins)

Preview Parameterized Functions

Remarks

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!

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.

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

Activity (30 mins)

Functions with Parameters and Return

Display: Use the activity slides for this lesson to guide the unplugged activity on Functions with Parameters and Return.

Slides                     Speaker Notes
Distribute: large and small sticky notes and envelopes. Hold off on distributing the Activity Guide until later in the activity.
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
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.
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

Do This: Read through the recipe as a class. Make sure everyone understands how it works before moving on.
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.
Say: This is a function! It should look familiar, but it has a few extra parts. This function has parameters. Parameters are used as plaeholders 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
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
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.
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.
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

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

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

Click for animation
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.

Click for animation
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.
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.
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 evalued and whatever is returned would be stored in the variable cakeCalculator.

Click for animation
Say: Here's how this looks in JavaScript. It's ok if it's a little confusing. You'll get to play with this in Code Studio in the next lesson. 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: We can also print to the console like so.

**Do This: Read through as a class.
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.

Wrap Up (10 mins)

Takeaways: Review the slide with the class.

  • 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

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


Assessment: Check For Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

Question: Read through the code and choose the correct answer.

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 2-AP-14 - Create procedures with parameters to organize code and make it easier to reuse.

CSP2021

AAP-3 - Programmers break down problems into smaller and more manageable pieces
AAP-3.A - For procedure calls: a. Write statements to call procedures. b. Determine the result or effect of a procedure call.`
  • AAP-3.A.1 - A procedure is a named group of programming instructions that may have parameters and return values.
  • AAP-3.A.3 - Parameters are input variables of a procedure. Arguments specify the values of the parameters when a procedure is called.
AAP-3.B - For procedure calls: a. Write statements to call procedures. b. Determine the result or effect of a procedure call.
  • AAP-3.B.4 - A procedural abstraction may extract shared features to generalize functionality instead of duplicating code. This allows for program code reuse, which helps manage complexity.
  • AAP-3.B.5 - Using parameters allows procedures to be generalized, enabling the procedures to be reused with a range of input values or arguments.
  • AAP-3.B.6 - Using procedural abstraction helps improve code readability.