[ 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.

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


Sponsored Link



Japanese English
Index
News From RINEARN
* VCSSL is developed by RINEARN.

Behind the Scenes of Creating an Assistant AI (Part 1: Fundamental Knowledge)
2024-10-07 - The first part of a series on how to create an Assistant AI. In this article, we introduce the essential knowledge you need to grasp before building an Assistant AI. What exactly is an LLM-based AI? What is RAG? And more.

Launching an Assistant AI to Support Software Usage!
2024-09-20 - We've launched an Assistant AI that answers questions about how to use RINEARN software and helps with certain tasks. Anyone with a ChatGPT account can use it for free. We'll explain how to use it.

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.

RINEARN Graph 3D Updated and Next Version (Ver.6) Development Has Begun!
2023-09-04 - We have released RINEARN Graph 3D Ver.5.6.34. In addition, we have initiated the development of the next major version, Ver.6! Details inside.

Commitment to Significantly Expand English Documentation with AI Assistance - Presenting a Behind-the-Scenes Look at the Actual Workflow
2023-05-28 - We have decided to expand our English documentation by leveraging AI technologies such as ChatGPT. We will provide a detailed explanation of our initiative.

Update to RINEARN Graph 3D: Enhanced API-related Features and More
2023-05-18 - We recently released the latest version of RINEARN Graph 3D, Ver.5.6.32. Explains the details of this update.

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