Parameters, Return, and Libraries - Lesson 3: Parameters and Return Practice

Overview

Students practice writing programs with parameters and return values by creating and debugging functions that use them.

Goals

Students will be able to:

  • Correctly set up a parameter in a function
  • Correctly set up a return value in a function
  • Write comments to explain the function purpose, parameters, and return values

Purpose

This is students' opportunity to practice using parameters and return values in a variety of contexts. The progression of levels builds in complexity from students merely calling functions with parameters to designing functions and debugging complex programs that use parameters and return values. This lesson should build students familiarity with these concepts as they prepare for the Make project in the next lesson.

Resources

Getting Started (5 minutes)

Quick 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?

Discuss: Have students share responses with a neighbor before discussing with the whole class.

Discussion Goal

Goal: Use this quick prompt to review what students should already have seen about parameters and return values while also prompting them to think about how they'll need to use those skills in today's lessons. You should use your remarks to review the fact that parameters and return help write neater programs, but they may be a little tricky at first to read and understand. That's why we practice!

Up to this point, students have only seen complete traversals - that is, traversals where every element of a list is accessed. Now that they've been introduced to return, students can build partial traversals where a list is only partially accessed. For example: A list could be traversed to find a specific element. Once that element is found, "found element x" is returned, and the traversal stops. While students don't actually need to build partial traversals themselves, they now should know how it theoretically could be created.

Remarks

  • Parameters and return values allow us to write code that is neater and works in a larger number of scenarios. We're going to have to practice reading, writing, and debugging code like this however, because it works a little a differently than the programs we've gotten used to writing. Let's dive in!
Teaching Tip

Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.

Activity (35 minutes)

Practice Time

Group: It is recommended that students work in pairs for this lesson.

Remarks

  • Today you're mostly going to practice what we've learned about programming with parameters and return values. In general we want to use the debugging process we've practiced all year, but here's some specific things to look out for in today's lesson.
    • Use output to call functions with parameters with different values to see how they run

Lesson 3 Apps 1-5: These Apps involve functions with parameters and return values, working in the console

  • Lesson3_App1: Calling a function multiple times with a single parameter
  • Lesson3_App2: Students follow a pattern to choose a randomCompliment from a list that is concatenated with a name passed through a parameter
  • Lesson3_App3: This is the same as Lesseon3_App1, except now the functions use return values. Again, students follow a pattern given in another working function.
  • Lesson3_App4: Students write a function that returns the season a month is in. By this point, students should be noticing the type of commenting that is happening above each function. This format previews how students will need to write comments for libraries later on in the unit.
  • Lesson3_App5: This level traverses lists to find the longest and shortest words. Parameters and return values are used.

Lesson 3 Apps 7-8: Students continue practicing, now working with apps

  • Lesson3_App6: Students debug and comment an ordering app. Completing this level will require students to read programs that use functions with parameters and return carefully.
  • Lesson3_App7: We return to the compliment code, this time reworking it into an app. Again students write their own comments, this time including comments for a return value. Students also complete the UpdateScreen function, which calls both of the functions with parameters.

Lesson3_App8: Students do a quick practice with the MOD operator.

Extension Opportunities:

  • Lesson3_App7: Add more greeting and compliment options to the lists

Wrap Up (5 minutes)

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?

Discuss: Have students share with one another before sharing with the whole class.

Discussion Goal

Goal: Use this opportunity to address any lingering questions or misconceptions in the room. You can also use this as a source of discussion topics to kick off the following lesson. As you lead the discussion, call out the many resources students have access to help when they're getting stuck.

Remarks

  • Working with parameters and return values can be tricky. We will get more practice tomorrow by making an app that uses lists to store information.

Assessment: Check for Understanding

For Students

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

Question

The procedure below will be called twice with two numbers provided as arguments to the parameter myNumber. Which of the following pair of numbers will mystery return the same value?

PROCEDURE mystery myNumber
final <- 0
IF myNumber > 100
    final <- 100
ELSE
    IF myNumber < 0
        final <- 0
    ELSE
        final <- myNumber
return final

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 2-AP-14
  • CSP2021: AAP-2.C.2, AAP-2.O.1, AAP-3.A.5, AAP-3.A.7, AAP-3.A.8, AAP-3.C.1, AAP-3.C.2

Next Tutorial

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