[ Prev | Index | Next ]
Japanese English

'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:

vcssl MyProgram.vcssl aaa bbb ccc

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 -

5

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 -

5

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.



Sponsored Link



Japanese English
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