Hello World
program HelloWorld { entrypoint main = HelloWorld.main; } component HelloWorld { method main(args: char[][]): char[] { return "Hello World!\n"; } }
Let's take a look at the source code for the HelloWorld
program.
The first thing to notice is the program
declaration. This declaration
describes the overall structure of the program for the compiler, including the
entrypoint(s). In this example, we declare the
main
entrypoint and set it to HelloWorld.main
. This
instructs the compiler which method is main starting point. Unlike Java
and other languages, Virgil does not require that the main method have a particular
name (such as "main"); instead, the programmer declares what method is the entrypoint
in the program declaration. In this example, we could easily have called our main
method start()
, or hello()
.
The second thing to notice is the declaration of the component HelloWorld
.
Components will be discussed in more detail later in this introduction. For now, you
can think of them as modules where related code and data is declared. Inside this
component we have a single method, main()
, which simply returns the
string constant "Hello World!\n"
. When the interpreter executes this
method, the value returned will be a reference to this string. The interpreter then
prints the string to the terminal and then exits.
Go back to the tutorial.