What is Scope?
You'll learn: What scope means and why a variable's location in your code determines where you can use it.
The Core Idea
Scope is the region of your program where a variable name is visible and can be accessed. Think of it like a variable's "neighborhood" — the area of code where that variable exists and can be used.
Just because you've declared a variable somewhere in your program doesn't mean you can use it everywhere. The rules of scope determine which parts of your code can "see" and use each variable.
An Analogy
Imagine your program as a building with different rooms. When you declare a variable inside a function, it's like writing a note and leaving it in that room. People in that room can read the note, but someone in a different room can't see it. The room represents the variable's scope — the boundaries within which that variable exists.
Why Scope Matters
Scope prevents naming conflicts and helps organize code. Two different functions can each have their own variable named count without interfering with each other because each variable only exists within its own scope.
When you declare a variable inside a function, that variable typically belongs to that function's scope. It's created when the function is called and disappears when the function returns. Code outside the function can't access it.
Example Scenario (Conceptual)
If you declare a variable inside a function, that variable is local to that function — it only exists within that function's scope. Any attempt to use that variable name outside the function will fail because the variable isn't visible there.
Key Takeaway: Scope defines the region of code where a variable name can be accessed — variables aren't universally visible throughout your entire program.