Section 7.8
Procedures
Learning Goals
Students will express an algorithm that uses sequencing without using a programming language (AAP-2.A)
Students will represent a step-by-step algorithmic process using sequential code statements (AAP-2.B)
Students will use abstraction to manage complexity in a program (AAP-3.A)
Students will determine the result of code segments (AAP-3.A)
Students will express an algorithm that uses selection without using a programming language (AAP-2.G)
AAP-2.A.1: An algorithm is a finite set of instructions that accomplish a specific task.
AAP-2.A.2: Beyond visual and textual programming languages, algorithms can be expressed in a variety of ways, such as natural language, diagrams, and pseudocode.
AAP-2.A.3: Algorithms executed by programs are implemented using programming languages.
AAP-2.A.4: Every algorithm can be constructed using combinations of sequencing, selection, and iteration.
AAP-2.B.1: Sequencing is the application of each step of an algorithm in the order in which the code statements are given.
AAP-2.B.2: A code statement is a part of program code that expresses an action to be carried out.
AAP-2.B.3: An expression can consist of a value, a variable, an operator, or a procedure call that returns a value.
AAP-2.B.4: Expressions are evaluated to produce a single value.
AAP-2.B.5: The evaluation of expressions follows a set order of operations defined by the programming language.
AAP-2.B.6: Sequential statements execute in the order they appear in the code segment.
AAP-2.B.7: Clarity and readability are important considerations when expressing an algorithm in a programming language.
AAP-2.G.1: Selection determines which parts of an algorithm are executed based on a condition being true or false.
Objectives and Description
The objective of this lesson is to both introduce students to procedural abstraction, a way to generalize procedures in a way that can handle multiple different inputs (within some specified parameters). These procedures can be used throughout our code to apply them to different situations or produce different results
Activities
Activity 7.8.1 (Budget 20 minutes)
Begin by engaging students with a think-pair-share activity where they are tasked with outlining a mundane step-by-step process from their daily lives.
For example, brushing your teeth is something everyone knows how to do when you mention it, but it is actually a process with a lot of steps:
Grab toothbrush
Wet the toothbrush
Apply toothpaste
Wet the toothbrush
Brush front of teeth
Brush back of teeth
Brush top of teeth
Repeat steps 5-7 as necessary for 2+ minutes
Scrub tongue
Rinse out mouth with water
As students share their processes with the class, make a note to them of how many steps each one is
Additionally, if students share a process that may need more steps or details to be accurate, ask the class what is missing.
After a few examples, engage the students in a discussion about how this might apply to computer science and coding
When we brush our teeth every day, do we need each and every step detailed to us or do we know all the steps just by thinking about "brushing teeth"
Similarly, in a script, if there is a set of actions we want executed multiple times, it is often simpler and more efficient to detail that set of actions as a procedure and call upon that procedure as needed rather than pasting the individual steps multiple times throughout the code.
This is the basis for custom functions and procedures in coding.
4. Any time after you have introduced slide 6, you can begin a discussion about selection. Use this example, Imagine you’re trying to decide what to wear for the day. You look outside and see that it’s raining. You have two options: wear a raincoat or wear a regular jacket. You decide to wear a raincoat because it will keep you dry in the rain. In this scenario, you have used selection to make a decision based on a condition (the weather). Selection is a fundamental concept in programming, but it’s also something we use in our everyday lives. We make decisions based on certain conditions all the time, whether we realize it or not.
Have students pair up and share an algorithm that uses selection without using a programming language.
Activity 7.8.2 (Budget 30 minutes)
Teacher will use their example script from lesson 7.7 to walk students through how to modify existing sections of code to work within the scope of a user-defined function.
Teacher should highlight the importance of switching out original code that indicates specific values with a information from the user-defined function
Example: replacing the values in the “measure” parameter of fitMedia(), makeBeat(), etc with a value that the user inputs into the function
Activity 7.8.3 (Budget 60 minutes)
Students will receive an example script that contains code for a song that must be reformatted using user-defined functions
Take special care when determining what parameters/arguments the user-defined functions will take in, and how these are used in the subsequent code.
There should not be any hard-coded measure inputs for fitMedia() or makeBeat()
The end result should be two user-defined functions that contain the original code that students identify as being either part of section A or B
These new user-defined functions will be called at the end of the script to construct a song with ABAB form.
Sections should cleanly transition and repeat
Activity 7.8.4 (Bonus)
Students will restructure their script from lesson 7.7 (after creating a new copy) by placing the existing code into a user-defined function for a section A, then creating a new section B to create a song with ABAB form (verse-chorus-verse-chorus)
Students should integrate loops with makeBeat to display understanding of code that has two layers of abstraction (custom function parameters and loop variables)