5.2. Main method#
The work that you do in this section will be done in a new directory under your
ValaProjects
directory called MainMethods
:abbr:
Create the
MainMethods
directory under yourValaProjects
directoryCreate file called
main.vala
inside theMainMethods
directory.
In the Hello World section, you wrote the following program:
public static void main () {
print ("Hello, world!\n");
}
The main method that above is the entry point of every Vala program. It is called by the program automatically.
Throughout this tutorial so far, we’ve used on version of the main method. There are different types of main methods that give you access to additional features that are useful for come complicated programs.
Here are some of the other main methods versions there are available:
5.2.1. Main method with exit code#
public static int main () {
print ("Hello, world!\n");
return 0;
}
If you look at the method signature in line 1, the main method has an int
return
type instead of a dev
return type. This means that you are required to return an
integer in this method.
In line 3, the value of 0
is returned. The value that is returned is an exit code.
Exit codes tell the operating system the exit status of the program.
0
means that you the program ran successfully without errorsAny non-zero value means that there was an error with the program. The meaning of each value is dependent on the operating system you are using.
5.2.2. Main method with arguments#
public static void main(string[] args) {
print (@"Arguments list length: $args.length\n");
print ("Arguments list:\n");
foreach (string arg in args) {
print (@"$arg\n");
}
}
If you compile and run the program, you’ll get a result that looks similar to this:
Arguments list length: 1
Arguments list:
./main
However, the output can change depending on how you call the program.
The value of the argument list is determined by each space seperating string is used when calling the program. There will always be at least one argument which is the path to the program executable.
The following arguments are space separated strings that appear after the location of the program.
Try run the program again but append a space then arg2 arg3
to the program’s path.
e.g: ./main arg2 arg3
.
The output of the program will look like this:
Arguments list length: 3
Arguments list:
./main
arg2
arg3
You can use the values in the arguments list in your program to modifiy its behaviour using data passed in from the environment it was called from.
There is also a version of the main
method that allows you to return an exit code too. The following code
will produce the same output:
public static int main(string[] args) {
print (@"Arguments list length: $args.length\n");
print ("Arguments list:\n");
foreach (string arg in args) {
print (@"$arg\n");
}
return 0;
}
5.2.3. No main method#
If you have a pretty simple program, you don’t even need to write a main method block. This code will compile and run just fine:
print ("No main method block here!\n");
print ("Hello world!\n");
The output of the program will be:
No main method block here!
Hello world!
Warning
In the Vala version this tutorial is for (0.56), you will receive a warning stating that “main blocks are experimental”.
So please use this feature with caution.
5.2.4. Summary#
Congratualations! You’ve now finished this chapter.
To recap, you’ve learned:
Thing 1
Thing 2
Thing 3
– INSERT CHAPTER END MESSAGE HERE –
Now, on to the next chapter!