Function scope
π Scenario: Imagine you are organizing a small event and you want to keep track of the number of guests inside the event hall. You will use functions to manage this count, but you want to understand how variables inside and outside functions behave.
π― Goal: You will create a variable to count guests, write a function that changes this count inside its own scope, and then see how the count changes outside the function.
π What You'll Learn
Create a variable called
guestCount and set it to 10Create a function called
addGuests that declares a local variable guestCount and sets it to 20Inside the function
addGuests, print the local guestCountOutside the function, print the original
guestCount variableπ‘ Why This Matters
π Real World
Understanding function scope helps avoid bugs when managing data in different parts of a program, like counting guests or tracking inventory.
πΌ Career
Many programming jobs require clear understanding of variable scope to write clean, bug-free code.
Progress0 / 4 steps