The Main Function
Next: Index,
Previous: Functions,
Index: Index
As already mentioned, as the programmer, you can choose to use the main function or not. The main function looks like any other function with a few rules!
1. The main function is void - it can never return a value
2. The main function EITHER takes no arguments or two arguments
The reason the main function might take arguments is to allow for command like arguments. Like C, of the two arguments it takes, the first one, an integer, is the number of command line arguments passed and the second one, an array of Strings, is the actual arguments. The first entry of the String array (in position) zero is the name of the program, and will be there regardless of whether there are any further command line arguments. The types of the two variables are AUTOMATICALLY infered by the compiler and need not be explicitly stated.
Examples
function void main[var a, var b]
{
print["Program name is ",b#0,"\n"];
var i;
for i from 1 to a - 1
{
print["Argument Number ",i," is ",b#i,"\n"];
};
};
This example displays the program name and then iterates through the arguments, displaying each one.
function void main[var a,var b]
{
var one:Int :: allocated[single[on[0]]];
var two:Int :: allocated[single[on[1]]];
proc 0 one:=pid[] * a;
proc 1 two:=pid[] * a;
proc 0 print["Old contents ",one,"\n"];
one:=two;
proc 0 print["New contents ",one,"\n"];
};
Lastly, the example might be a little pointless - there are two variables, one and two allocated to processes 0 and 1 respectively. Each process performs some calculation based on their PID and the number of arguments. Process 0 displays its calculated value of one. Process 1 then sends via, P2P communication, the contents of variable two to process 0 which assigns it into variable one. Process 0 then displays the new contents of one.
Last Modified: August 2008