Parameters, Return, and Libraries - Lesson 2: Parameters and Return Investigate
Overview
In this lesson students work with partners to investigate two different apps that use parameters and return values. Students are also introduced to the mod operator as part of one of the apps that they use.
Goals
Students will be able to:
- Identify situations in which a function with a parameter or return value would be necessary
- Explain the benefits of using a function with a parameter or return value in the context of a specific program
- Modify programs that use functions with parameters and return
- Use the modulus operator in a program
Purpose
As with all Investigate lessons, this is an opportunity for students to dig into programs that use a new concept, in this case parameters and return, in the context of working apps. Encourage students to read the code carefully, discuss their findings with classmates, make connections to the Explore activity from the day before, and start making simple modifications to the program. Students will not leave this lesson as experts in writing programs with parameters and return, but they should understand the high level context and know they can refer back to the code in these investigate projects when they need help getting unstuck in the coming Practice and Make lessons.
Resources
Getting Started (5 minutes)
Who is Clean Code For?
Prompt: Are clean and organized programs more useful for computers or people? Why? Try to give examples from programs you've written or seen in this class.
Discuss: Have students silently journal their response, then discuss with a partner, and finally share with the class.
Discussion Goal
Goal: Parameters and return values allow students to write programs that are more organized and cleaner. Naming functions helps students write programs that read more like descriptions of what they do, and they also help students reuse code.
This quick discussion sets up many of these realizations. Students will not leave this lesson able to "do more" than previously. Instead they'll be able to write programs that are cleaner, better organized, and better able to be used elsewhere. Long term, writing programs like this will actually help students write more complex code, but because they as programmers will better be able to understand their programs and reuse the code they write. This quick prompt does not need to cover all of those points and should just be a reminder of things they've already seen. Clean code is for humans, but in the long term it helps them write better programs.
Remarks
- Clean programs may sometimes run "better" but usually they're just helpful for us as we read them and write them. In other words, clean code is for people, but in the long run writing good code can help us write larger and more complicated programs because we can just read them and understand what they're doing.
- Today we're going to learn more about parameters and return values. Something important to notice is that they might not actually change the way that your programs run, but they definitely will change the way we read and write programs. In general they'll make programs cleaner and easier to understand, but let's go look at some examples.
Activity (35 minutes)
Do This: Take a few minutes to review the takeaways from the previous lesson.
Group: Place students with partners
Do This: Students navigate to Lesson2_App1 and read and respond to the questions there. If students have extra time, they can try the modify activity.
Teaching Tip
Make sure you leave time to bring the class back together for discussion and code walkthroughs. There's a lot to dig into with these Investigations.
- How does
Calculate()
work? - What are the arguments passed through the parameter in
Calculate()
when it is called? - What types of data does the parameter require in the
Calculate()
function? Where can you find that information? - What is returned? What type of data?
Discuss: As a class, walk through the questions and the code. Make sure students understand the parts of the function.
Teaching Tip
Students need to know how the MOD operator works for the AP CSP exam. The explanations here are enough to help them answer those questions.
There are many wonderful and interesting uses of MOD in apps, but students are not expected or required to use the operator themselves when programming apps for this course.
Do This: Now look together at lines 64-70. Students discuss with a partner how they think the MOD operator works.
Do This: The MOD operator is just like any other operator - it takes two numbers, processes them, and then returns a value. Click through the animations one by one as students give answers to each arithmetic statement.
Do This: Give students a few minutes to practice using the MOD operator with a partner, solving the problem on the screen. Then click through to see the answer.
Remarks
- MOD can be a little tricky, but here's what you need to remember: it's the remainder that is left after a number is divided by another number.
- Why is this useful? A common usage is to determine if a number is even or odd. If you divide any number by two and there is no remainder, the number is even! You can also use MOD to determine if a number is divisible by another number.
- Now let's get back to another app investigation with functions with parameters and return values.
Do This: Students navigate to Lesson2_App2, read lines 37-43 and lines 160-164 and discuss with their partner what's happening in these lines.
Discuss: Read through the lines together as a class, and clear up any misconceptions.
Do This: Students carefully read the function on lines 45-69 before explaining to their partners how it works. If students have extra time, they can try the modify activity.
Discuss: Talk through the app together as a class. Make sure students understand all the parts of the function and understand where to look in the comments for information.
Wrap Up (5 minutes)
Review: Discuss the takeaways.
Takeaways
- Extracting shared features to generalize functionality is known as procedural abstraction.
- Using parameters allows the functions (also called procedures) to be generalized.
- Using procedural abstraction helps improve code readability.
- Procedural abstraction manages complexity by allowing for code reuse.
- For example: the function
move(id, direction)
could be used to move an element in any direction, rather than writing separate functions for each direction.
- For example: the function
Assessment: Check for Understanding
For Students
Open a word doc or google doc and copy/paste the following question.
Question
What are the benefits of writing functions that use parameters and return? Try to list at least two.
Standards Alignment
- CSTA K-12 Computer Science Standards (2017): 2-AP-14, 3B-AP-23
- CSP2021: AAP-2.C.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 3, which describes Learn about parameters and return.