AP CSP CodeX - Functions and Global Variables

Functions and Global Variables Lesson Plan

Functions and Global Variables

Students learn when a variable should be global, and how to avoid a global variable by calling one function from another.

⏰ 45 min 🎯 Grades 9-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

Students work on computers in the CodeSpace Sandbox. This lesson is not part of a mission. Students review functions, parameters and local variables, then learn about global variables using programs from Mission 6 (Heartbeat), Mission 7 (Billboard) and Mission 9 (Game Spinner). Next, they add a function to their PT_Practice2 program that shows the results of the game. They count the correct guesses and pass the count to the new function as a parameter, instead of using a global variable.

🎯 Mission Goal: Students will learn how to work with global variables in a function, and how to avoid a global variable by using a function to call another function.

🎯

Learning Targets

  • I can determine when a variable should be global.
  • I can determine when a function needs a global declaration.
  • I can use and change a global variable in a function.
  • I can call a function with parameters from another function.
💡

Key Concepts

  • A program will sometimes use a global variable that is defined in the main program and can be used throughout the program.
  • If the value of a global variable is changed in a function, it needs a global declaration.
  • A function can call another function that has a parameter. This is a way to avoid global variables.
✅

Assessment Opportunities

☑️

Success Criteria

  • Answer questions about when and how to use global variables in a function
  • Use a variable as a counter
  • Call a function with a parameter from another function
📁

Digital Resources

🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
🚀

Extensions & Cross-Curricular

ExtensionStudents change the program to count correct guesses with a global variable instead of a parameter, then compare the two versions and explain which one is easier to read.
ExtensionStudents add a second counter for incorrect guesses and pass both counts to results().
MathStudents use the counter to calculate the percent of correct guesses and display it in results().
Lang ArtsStudents write a short explanation, in the style of a Create PT written response, of why passing a parameter can be a better choice than using a global variable.
🔤

Vocabulary

▾
Function - (review) a named set of instructions that accomplishes a task. (A named chunk of code you can run anytime just by calling its name; also called a procedure.)
Procedural abstraction - The technique that breaks down complex tasks into smaller, more manageable procedures.
Global variable - A variable defined outside a function that retains its value throughout program execution and can be used anywhere in the code.
📐

Standards

▾

CSTA Standards

The Create PT Prep unit reviews the CSTA Standards for Units 1, 2 and 3.

AP CSP Framework

AAP-1.A - Represent a value with a variable.
AAP-1.B - Determine the value of a variable as a result of an assignment.
AAP-3.A - Write statements to call procedures.
AAP-3.C - Develop procedural abstractions to manage complexity in a program by writing procedures.
Computational Thinking Practice 3.B - Use abstraction to manage complexity in a program.
📝
Preparing for the Lesson
  • Prepare the Activity Guide to give to students. It is used for notes during the lesson, the warm-up questions, and the wrap-up questions.
  • Make sure students have the PT_Practice2 program from earlier in this unit completed and accessible, or have the original code ready to give them.
  • If you have a word wall, or another form of vocabulary presentation, prepare the terms.

🧑‍🏫
Teacher Notes
  • Students complete this lesson on the computer, using the Sandbox in CodeSpace for programming. This lesson is not part of a mission.
  • Students add a function to the PT_Practice2 program.
  • The best experience will come from students modifying their own code. However, we want all students to be engaged, so you can give them the original code to modify if needed. The program was completed earlier in this unit, in PT Practice 2.
  • Follow the slides for instructions and guidance. Additional help is provided in the Lesson Outline below.
  • Solution code for the modified program is provided.
🗺️

Lesson Outline

This outline gives one way to present the lesson.

🗣️Warm-up

Use a discussion strategy, like journaling, working at boards, selecting random students, or a form of think-pair-share.

Teaching tip: Use the three warm-up questions on the Activity Guide as a way to start the lesson. You can use individual reflection, group discussion, etc.
💻Activity - Global Variables

💻 Students review functions and parameters, then learn about global variables.

Teaching tip: Slides 2-7: The slides review functions, parameters and local variables. The first example is one they have not used in code, but they should be able to tell what it does and how it works. The second example is from Mission 9: Game_Spinner. You can show the entire program on a screen, or have students open the program if you think it is helpful. The function being discussed is on slide 6.
Teaching tip: Slides 8-10: The slides introduce global variables: what they are and when they might be used.
Teaching tip: Slides 11-13: The slides show an example of using a global variable with the program from Mission 6: Heartbeat (heart2).
Teaching tip: Slides 14-16: The slides show an example of using a global variable with the program from Mission 7: Billboard. The example is from the mission, and not from Unit 3 where parameters are added. You can show the program on the screen or have students open the program if you think it is helpful. You can change the code and see what happens. For example, take out "global choice" and see what error happens. Add "global choice" to the other function, and see what happens. Take some time to experiment with the code.

✏️ Have students answer the three questions on the Activity Guide for this activity.

💻Activity - Avoiding a Global Variable

💻 Students add to their PT_Practice2 program in the CodeSpace Sandbox.

Teaching tip: Slide 17: This slide starts the discussion of how a global variable can be avoided. Students may not get why. Why not have everything global? Good discussion point!
Teaching tip: Slide 18: Example #3 uses the PT_Practice2 program. Students should open the program in the Sandbox of CodeSpace. Students add a variable to count correct guesses. Since the variable "count" is already used in the for loop, use a different variable name, like "correct".
Teaching tip: Slides 19-21: Students add another function with a parameter to the program that will show results. They call the new function in the current function, and pass "correct" as a parameter to the new function.
Teaching tip: Slides 22-26: Students add functionality to the new results() function to meet the requirements of the Create PT (the function requirements, not the list requirements).

✏️ Have students answer the two questions on the Activity Guide for this activity.

Teaching tip: Slides 27-28: Summary of the lesson. Students have learned about using global variables, and also have another way to program a function that meets the Create PT requirements.
🧑‍🤝‍🧑Wrap-Up (optional)

You can use the wrap-up questions on the Activity Guide as a way for students to reflect on their lesson and/or share out and wrap up the lesson.

✅ Formative assessment:

  • Daily reflection journal
  • Exit ticket or group review
  • Activity Guide completion
  • Program completion