AP CSP CodeX - Functions and Global Variables
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.
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
- Functions and Global Variables Activity Guide
- Completed PT_Practice2 program with the new results() function
- Daily reflection journal
- Exit ticket or group review
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
- ▸Functions and Global Variables slides
- ▸Functions and Global Variables Activity Guide
- ▸Optional: Survival Guide
- ▸Code Solutions and Assignment Answers - Teacher Dashboard > Educator Resources button
Classroom Materials
- ▸CodeX
- ▸USB-C cable
- ▸Computer
Extensions & Cross-Curricular
🔤
Vocabulary
▾
📐
Standards
▾
CSTA Standards
The Create PT Prep unit reviews the CSTA Standards for Units 1, 2 and 3.
AP CSP Framework
- 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.
- 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.
Use a discussion strategy, like journaling, working at boards, selecting random students, or a form of think-pair-share.
💻 Students review functions and parameters, then learn about global variables.
✏️ Have students answer the three questions on the Activity Guide for this activity.
💻 Students add to their PT_Practice2 program in the CodeSpace Sandbox.
✏️ Have students answer the two questions on the Activity Guide for this activity.
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