'main' Function
In this section, we will explain the 'main' function, which is automatically called when a program is executed.
スポンサーリンク
What is The 'main' Function?
Some Functions Are Automatically Called
In the previous section, we defined functions and called them from within our program. However, some functions are automatically called.
Specifically, a function will be automatically called at certain times if it is defined with a name and arguments that match those specified by the language or its libraries. There are several such functions in VCSSL.
The 'main' Function Called at Program Execution: Describing the Overall Processing Flow
A typical example of this is the "main function" we are discussing here. This function is automatically called when the program is executed. In other words, if you define the main function in your program, it will always be called. Typically, the main function is used to describe the overall processing flow of the program.
As programs become more complex, understanding their overall processing flow becomes more challenging. Therefore, it is effective to divide the processing into multiple functions for organization. As you organize in this way, you naturally want to consolidate the outermost processing, which calls these functions, into some kind of function. This is precisely where the main function plays a crucial role.
The 'main' Function Not Mandatory in VCSSL
In VCSSL, the main function is not mandatory because you can write any processing outside of functions. You are free to either write the overall processing directly from the beginning of the program (outside of functions) or consolidate it in the main function. The former is convenient for short programs. Conversely, in longer and more complex programs with many functions, the latter likely enhances readability.
However, in some languages like C, the definition of the main function is mandatory.
The Forms of the 'main' Function
VCSSL offers two forms of the main function. The simplest form is as follows:
The second form accepts an array of strings as its parameter:
The latter form is convenient when creating programs for use on the command line. Using the vcssl command to execute a program with command line arguments passes those arguments to the 'args' parameter of the main function. For example:
When executed like this, an array of strings with three elements containing "aaa", "bbb", and "ccc" is passed to the 'args' parameter of the main function.
Example of Using the 'main' Function
Now, let's see how to use the main function in practice.
First, let's consider an example without using the main function:
- Execution Result -
This program directly writes the processing from the beginning. Since the processing content is very short and simple, it is readable as it is.
Next, let's refactor the calculation part of the above program into a function and consolidate the overall processing flow into the main function:
- Execution Result -
In this refactored program, the processing is organized into functions. When this program is executed, the main function is automatically called first. Then, in the main function, the 'calc' function is called, resulting in the same processing as before.
With programs organized into functions like this, humans can understand the content in the following way:
- 1. First, read the main function to grasp the overall processing.
- 2. Read the functions called within the main function to understand the detailed processing.
- 3. Read further into the functions called within those functions to understand even finer details of the processing.
This "from the big picture to the details" approach makes it convenient to delve into longer and more complex programs.
Order of Processing: Functions Outside and 'main' Function
Finally, let's supplementary explain the order of processing of code having a little "odd" structure.
Consider a program like the following, where "processing A" and "processing B" are scattered outside the main function:
Although it's a somewhat unconventional way of writing, it's syntactically valid in VCSSL. Now, what order will these processes be executed in?
To clarify the processing order in such cases, VCSSL has a rule that the main function is called "after all processing outside of functions is completed." Therefore, the answer is:
- 1. Processing A
- 2. Processing B
- 3. Processing in the main function
This rule may seem confusing at first glance because the visual order in the program does not match the execution order.
However, in practice, this rule is convenient. For example, variables declared and initialized after the main function are guaranteed to be initialized before they are used within the main function. This rule also convenient when declaring global variables/constants that require some complex calculations during initialization. You can describe such calculations just after the declaration statements of the variables/constants, because they are assured to be perfomred before the execution of the main function, by this rule.
Hence, this rule is in place for practical reasons.
» 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