Lesson 3: Parameters and Return Practice
45 minutes
Overview
Students practice writing programs with parameters and return values by creating and debugging functions that use them.
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.
Standards
AAP-2 - The way statements are sequenced and combined in a program determines the computed result. Programs incorporate iteration and selection constructs to represent repetition and make decisions to handle varied input values.
AAP-2.C - Evaluate expressions that use arithmetic operators.
- AAP-2.C.2 - The exam reference sheet provides a MOD b, which evaluates to the remainder when a is divided by b. Assume that a is an integer greater than or equal to 0 and b is an integer greater than 0. For example, 17 MOD 5 evaluates to 2.
AAP-2.O - For algorithms involving elements of a list: a. Represent using iterative statements to traverse a list. b. Determine the result of an algorithm with list traversals.
- AAP-2.O.1 - Traversing a list can be a complete traversal, where all elements in the list are accessed, or a partial traversal, where only a portion of elements are accessed. EXCLUSION STATEMENT (EK AAP-2.O.1): Traversing multiple lists at the same time using the same index for both (parallel traversals) is outside the scope of this course and the AP Exam.
AAP-3 - Programmers break down problems into smaller and more manageable pieces. By creating procedures and leveraging parameters, programmers generalize processes that can be reused. Procedures allow programmers to draw upon existing code that has already been tested, allowing programmers to write programs more quickly and with more confidence.
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.5 - The exam reference sheet provides procName (arg1, arg2, …) as a way to call PROCEDURE procName(parameter1, parameter 2, …) which takes zero or more arguments; arg1 is assigned to parameter1, arg2 is assigned to parameter2, and so on.
- AAP-3.A.7 - The exam reference sheet provides the RETURN(expression) statement, which is used to return the flow of control to the point where the procedure was called and to return the value of expression.
- AAP-3.A.8 - The exam reference sheet provides result ß procName(arg1, arg2, …) to assign to result the “value of the procedure” being returned by calling PROCEDURE procName(parameter1, parameter2, …)
AAP-3.C - Develop procedural abstractions to manage complexity in a program by writing procedures.
- AAP-3.C.1 - The exam reference sheet provides PROCEDURE procName(parameter1, parameter2, …){<block of statements>}which is used to define a procedure that takes zero or more arguments. The procedure contains block of statements.
- AAP-3.C.2 - The exam reference sheet providesPROCEDURE procName(parameter1, parameter2, …){ <block of statements> RETURN(expression)} which is used to define a procedure that takes zero or more arguments. The procedure contains block of statements and returns the value of expression. The RETURN statement may appear at any point inside the procedure and causes an immediate return from the procedure back to the calling statement.
AP - Algorithms & Programming
- 2-AP-14 - Create procedures with parameters to organize code and make it easier to reuse.
Agenda
Objectives
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
Preparation
- Review the programming levels students will complete
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the teachers
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 minutes)
Quick Warm Up
Discuss: 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?
Have students share responses with a neighbor before discussing with the whole class.
Discussion Goal: Use this quick discussion to review what students should already have seen about parameters and return values while also discussing 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!
Activity (35 minutes)
Practice Time
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.
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 console.log to call functions with parameters with different values to see how they run
- Slow down the code with the speed slider to see how the code is running once your function is called
Levels 1-5: These levels involve functions with parameters and return values, working in the console
- Level 1: Calling a function multiple times with a single parameter
- Level 2: Students follow a pattern to choose a random Compliment from a list that is concatenated with a name passed through a parameter.
- Level 3: This is the same as Level 1, except now the functions use return values. Again, students follow a pattern given in another working function.
- Level 4: 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.
- Level 5: This level traverses lists to find the longest and shortest words. Parameters and return values are used.
Levels 6-8: Students continue practicing, now working with apps
- Level 6: Students debug and comment an ordering app. Completing this level will require students to read programs that use functions with parameters and return carefully.
- Level 7: 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. - Level 8: Students practice writing a
checkCorrect
function with parameters in making a quiz app function. The app uses data from theRoot: 100 Influential African-Americans dataset.
Extension Opportunities:
- Level 7: Add more greeting and compliment options to the lists
- Level 8: Use a different dataset in place of theRoot dataset.
Wrap Up (5 minutes)
Discuss: 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?
Have students share with one another before sharing with the whole class.
Discussion 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
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: 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?
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.