Scope and Blocks
In this section, we'll explore the concepts of scope and blocks, which are rules for referencing variables with the same name.
Sponsored Link
Variable Names May Duplicate in Long Programs
When writing long programs, you may sometimes end up using hundreds of variables. In such cases, creating unique names for each variable can be quite cumbersome.
While meaningful names are recommended for important variables, it's common to reuse short names, like one or a few characters, for temporary utility variables.
However, when multiple variables with the same name coexist, it can become unclear which variable is being referenced without any rules for the call. Hence, programming languages have the rules for determining it, so it is important to understand such rules, and effectively utilize them for writing readable code.
We'll explain the rules in VCSSL, but note that many of these are common in C-like languages as well.
Scope: The Accessible Range of a Variable, and Blocks: How They Control Scope and Priority
For example, when your family calls your name inside your house, you can immediately tell that you've been called. However, there are many people with the same name all over the world. Nevertheless, the reason you can determine that "you've been called" is because it's inside your house.
Just like your "house," there is something called "scopes and blocks" in a program that determines the range and priority of variable calls.
Blocks
For instance, try running the following program:
- Execution Result -
When you execute this, it will display 3.
In the above program, the region delimited by the symbols "{" and "}" is called a block.
In this case, we're calling the variable i with the print function, and you can see that it retrieves the value of the i variable inside the block. This is similar to calling a name inside a house, and someone inside the house responds. In other words, "build a house using a block."
A block increases the priority of a variable declared within it. This remains true even when blocks are nested hierarchically. In such cases, when you refer a variable at a certain point, it is first searched for within the nearest block from the referring point. If the variable with the desired name is not found within the block, it will then be searched outer blocks.
Scope
Next, let's talk about scopes. In simple terms, a scope is the range where a variable can be accessed. As shown in the example above, the scope of variables declared inside a block is limited to that block. It's like "speaking to someone inside a house from outside, and they don't notice you." Let's try it out for real. Please execute the following program:
- Execution Result -
In this case, the program displays an error and terminates. Referencing a variable from outside its scope is never possible.
However, while blocks delimit and restrict the scope of variables inside to prevent them from "leaking outside," they don't impose any limitations on the scope of variables outside. In other words, it is possible to refer variables from inside a block that are outside of it. Let's try it out:
- Execution Result -
In this case, the variable i outside the block is indeed called, and it displays 2.
Global Variables and Local Variables
Variables declared inside a block are referred to as "local variables." Conversely, variables declared outside all blocks are known as "global variables."
Local variables can only be accessed within their respective blocks and are effectively non-existent outside these blocks. They can be considered temporary variables used exclusively within the block.
On the other hand, global variables can be accessed from anywhere, making them very "convenient", for better or worse. However, if you use too many global variables, it can make the program very harder to read. That's one drawback to keep in mind.
» How we translated this page
- What is VCSSL? - Features of VCSSL
- Let's Get Started with VCSSL! - Setting Up the Environment
- Variables
- Data Types
- Arithmetic Operations
- Comparison Operations
- Logical Operations
- Scope
- Control Structures
- Arrays
- Vector Operations
- Functions
- "main" Function
- System Functions and Constants
- Standard File I/O