Lesson 2: Parameters and Return Investigate
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.
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.
Warm Up (5 mins)
Activity (35 mins)
Wrap Up (5 mins)
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
- Review the two apps that students will investigate in the lesson and make sure you understand the goals of the discussion prompts
Heads Up! Please make a copy of any documents you plan to share with students.
For the Teachers
- CSP Unit 7 - Parameters, Return, and Libraries - Presentation
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)
Who is Clean Code For?
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.
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.
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 mins)
Do This: Take a few minutes to review the takeaways from the previous lesson.
Group: Place students with partners
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.
Do This: Students navigate to Level 2 and read and respond to the questions there. If students have extra time, they can try the modify activity.
- 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 upgradeClickerCheck() 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.
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 30-36. 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.
MOD can be a little tricky, but here's what you need to remember: it's the reaminder 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 Level 3, read likes 1-14 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 15-34 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 mins)
Goal: Students are introduced to procedural abstraction on this slide, a concept they will return to throughout the unit. Spend a few minutes discussing what "generalize" means - focusing on how a single function can be used to accomplish many different tasks by using parameters.
Review: Discuss the takeaways on the Wrap Up slide.
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: What are the benefits of writing functions that use parameters and return? Try to list at least two.
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.
- 3B-AP-23 - Evaluate key qualities of a program through a process such as a code review.
AAP-2 - The way statements are sequenced and combined in a program determines the computed result
AAP-2.C - Evaluate expressions that use arithmetic operators.
- AAP-2.C.3 - The exam reference sheet provides the arithmetic operators +, -, *, /, and MOD. ● a + b● a – b● a * b● a / b● a MOD bThese are used to perform arithmetic on a and b. For example, 17 / 5 evaluates to 3.4.
AAP-3 - Programmers break down problems into smaller and more manageable pieces
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.