Define custom functions to organize programs
Time: 40+ mins
IntroductionIn this lesson, students will define custom functions to create more structured programs.
- Function: A named sequence of instructions that performs a specific action. Functions are usually used when the same instructions must be repeated or when the same sequence of instructions must manipulate different inputs.
- Function Definition: A function must be defined before it can be called and function names must be unique and follow the variable name rules. In Python, the function definition is started by using the key word “def,” the name of the function, parentheses, and a colon. Here is what the syntax looks like:
After the “def” statement, you can write a sequence of indented instructions that the function should perform when it is called. For example,
- Function Call: After a function is defined, you can use the name of the function to execute the instructions in the function. If there are no parameters in the function, the function name should have empty parentheses after it. For example,
If there is a parameter, the values for the parameters should be listed in the parentheses, separated by commas. For example,
- Parameter: A function takes arguments and stores the values in variables called parameters. A parameter variable is used inside the function’s sequence of instructions. Functions can have multiple parameters that are separated by commas. For example,
- Return Value: The value that a function outputs using the return keyword. The return value can be stored in a variable when the function is called.
- Default Parameter: A default value that is placed in the parameter when no argument is entered when the function is called.
- Global Variable: A variable that is defined outside the function but the value of the variable can be read from within the function. For example,
If the global variable is defined as global inside the function, then the function can also change the value of the variable. For example,
- Define and call their own functions
- Create multiple functions in one program
- Identify when to use different parameters in their functions
- Use global variables to store values that are changed in functions
- Computers (1 per student) with student account access to Tynker.com
Warm-Up (5 minutes)1. Explain to your students that the purpose of today’s warm-up is to get them thinking about sequences and functions.
2. As a class, brainstorm simple everyday tasks that requires the same instructions to be repeated. For example: chewing a piece of food.
3. Have students take out a piece of paper and a pencil.
4. Tell them to select an everyday task from the list and order the steps for that task.
5. Discuss your students’ sequencing lists and expected outcomes.
6. Explain that functions are usually used when the same instructions must be repeated or when the same sequence of instructions must manipulate different inputs, which we will explore in today’s lesson.
Activities (35 minutes)Facilitate as students complete the Functions modules on their own:
1. Functions (Tutorial)
- Students will read a short document that explains a function, which is a sequence of instructions that performs a specific computation.
- Optional: Ask students, “If you run the program in the first example, nothing happens. Why?” (In the first example, you told the computer how to perform the function, but haven’t told it to actually sing the song.)
- Are students struggling with the “Compliment generator” puzzle? Give a hint: Remind students that they need to call the function. If students are still struggling, tell them to use the code in the previous example as a reference.
- Tell students to click the “Next” button (located at the bottom of the document) to move on to the next module.
- In this module, students will read a short document that explains multiple functions.
- In the provided example, check that students are not including a space after the question mark (?). Also make sure students are using correct capitalization and are correctly spelling the villain’s name. Otherwise, the program will not run.
- Are students struggling with the “Hello and goodbye” puzzle? Give a hint: Tell students to use a “for” loop to repeatedly call the functions they define. Tell them that part of their solution might look something like this:
for x in range(10):
- In this module, students will read a short document that explains parameters.
- Optional: Before students select the “play” button in the examples, ask them to write down what they think the code will output.
- Are students struggling with the “Print square” puzzle? Give a hint: Remind students that the square of n can be expressed as: n * n
- Are students struggling with the “What to wear” puzzle? Give a hint: Tell students to use code from the “Multiple parameters” example as a reference. Also, tell students to include the less than (<) and less than or equal to (<=) comparison operators in their solution.
- In this module, students will read a short document that explains return values.
- Check that students are exploring the “Try This” activity, which encourages students to fix the code in the “Multiple return instructions” example.
- Are students struggling with the “Greatest of five” puzzle? Help them get started by providing them the first two lines of code:
def greatestOfFive(a, b, c, d, e):
greatest = a
From there, encourage them to use comparison operators and “if” statements as part of their solution.
- In this module, students will read a short document that explains default parameters.
- Are students struggling with the “Print the sandwich” puzzle? Encourage students to use an “if” statement. Also, remind students that if no sauce is specified, the program should not print any sauce. So part of their solution might look like this:
sauce = “”
- In this module, students will read a short document that explains global variables.
- Check that students are exploring the “Try This” section, which encourages students to remove the global age instruction (age = age + 1) from the provided code. How did this change affect the output?
- Are students struggling with the “Unique letter” puzzle? Direct their attention to the provided hint and tell them to use the code from the “Unique ID” example as a reference.
- In this module, students will read a short document that explains testing functions.
- This module does not contain puzzles.
- In the second example, students will need to convert the number to a string, then measure its length. If needed, encourage students to brainstorm possible solutions with a neighbor.
- This quiz requires students to apply concepts from this lesson to solve 3 different puzzles. There are no multiple choice questions.
Discussion Questions/Follow-Up Activities (20 minutes)Lead a discussion with your students:
- Who can give an example of how you used a function in today’s lesson?
- Using your own words, who can describe what a parameter is?
- True or false: A function can have multiple return instructions. (True, but it can only return one value)
- What’s one advantage of using functions in your program? (Functions can break down complex tasks into a composition of simpler ones.)
- CCSS-ELA: SL.8.1, RI.9-10.3, RI.9-10.6, RI.11-12.3, RI.11-12.6, L.9-10.3, L.9-10.6, L.11-12.3, L.11-12.6
- CCSS-Math: HSN.Q.A.1, HSN.Q.A.2, HSN.Q.A.3, MP.1, MP.2, MP.4
- CSTA: 2-AP-11, 2-AP-12, 2-AP-13, 2-AP-14, 2-AP-15, 2-AP-17, 3A-AP-17, 3B-AP-11, 3B-AP-12
- CS CA: 6-8.AP.11, 6-8.AP.12, 6-8.AP.13, 6-8.AP.14, 6-8.AP.15, 6-8.AP.17, 9-12.AP.12, 9-12.AP.14, 9-12.AP.16
- ISTE: 1.c, 1.d, 4.d, 5.c, 5.d
- 8 Activities
- 1 Completion Badge