[ Prev | Index | Next ]

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 -

3

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 -

Error: The variable \"i\" is not declared.

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 -

2

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.

Acknowledgement: We greatly appreciate the cooperation of two ChatGPT AIs in translating this article!
» How we translated this article


Sponsored Link



Index
News

Software Updates: Command Expansion in RINEARN Graph, and English Support in VCSSL
2024/02/05 - We updated our apps. This updates include "Enhancing the Command-Line Features of RINEARN Graph" and "Adding English Support to VCSSL." Delves into each of them!

Inside the Repetitive Execution Speedup Impremented in Vnano Ver.1.1
2024/01/17 - Delves into the update in Vnano 1.1 from a developer's viewpoint, providing detailed insights into the specific improvements made to the internal structure of the script engine.

Scripting Engine Vnano Ver.1.1 Released: Dramatic Speed Improvement for Repetitive Executions of the Same Content
2023/12/22 - Released the Vnano script engine Ver.1.1. In this version, we've made significant enhancements in processing speed by reducing the overhead of handling requests. Explains the details.

Updated Contents
Circular Wave Animation

Draws the circular wave as 3D animation, under the specified wave parameters.
2022/12/14
Sine Wave Animation

Draws the sine wave as animation, under the specified wave parameters.
2022/11/26
Tool For Converting Units of Angles: Degrees and Radians

A GUI tool for converting the angle in degrees into radians, or radians into degrees.
2022/11/22
Connector Fatal Exception - Specification
The unchecked exception thrown when errors have occurred, caused by incorrect implementations (might be bugs).
2022/09/26
Connector Exception - Specification
The checked exception thrown when errors have occurred, cause by expected normal problems.
2022/09/26