Main function (programming)
From Wikipedia, the free encyclopedia
- See also: Entry point
In some programming languages, the main function is where a program starts execution.
It is generally the first user-written function run when a program starts (some system-specific software generally runs before the main function), though some languages (notably C++ with global objects that have constructors) can execute user-written functions before main runs. The main function usually organizes at a high level the functionality of the rest of the program. The main function typically has access to the arguments given to the program at the command line.
Contents |
In C and C++, the function prototype of the main function is one of the following:
int main(void); int main(); int main(int argc, char *argv[]);
The parameters argc and argv respectively give the number and value of the program's command-line arguments. Other platform-dependent formats are also allowed by the C and C++ standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:
int main(int argc, char *argv[], char *envp[]);
The value returned from the main function becomes the exit status of the process, though the C standard only ascribes specific meaning to two values: EXIT_SUCCESS (traditionally zero) and EXIT_FAILURE. The meaning of other possible return values is implementation-defined.
The name main is special; normally every C and C++ program must define one and only one function of that name.
main must be declared as if it has external linkage; it cannot be declared static.
In C++, main must be in the global namespace (i.e. ::main) and cannot be a (class or instance) member function.
Java programs start executing at the main method, which has the following method heading:
public static void main(String[] args) public static void main(String... args)
Command-line arguments are passed in args. As in C and C++, the name "main" is special. Java's main methods do not return a value directly, but one can be passed by using the System.exit() function.
When executing a program written in C#, the CLR searches for a static method marked with the .entrypoint IL directive, which takes either no arguments, or a single argument of type string[], and has a return type of void or int, and executes it [1].
static void Main(); static void Main(string[] args); static int Main(); static int Main(string[] args);
Command-line arguments are passed in args, similar to how it is done in Java. For versions of Main returning an integer, similar to both C and C++, it is passed back to the environment as the exit status of the process.
Using GNAT, the programmer is not required to write a function called main; a source file containing a single subprogram can be compiled to an executable. The binder will however create a package ada_main, which will contain and export a C-style main function.
In Haskell, there may be a main name bound to a value of type IO (). IO is a monad, which is used to separate side-effects from purely functional areas of the program. Grossly simplified and analogised, the main value serves as the program's entry point.
Command line arguments are not given to main; they must be fetched using another IO action.
In Pascal, the main procedure is the only unnamed procedure in the program. Because Pascal programs have the procedures and functions in a more rigorous top-down order than C, C++ or Java programs, the main procedure is usually the last procedure in the program. Pascal does not have a special meaning for the name "main" or any similar name.
procedure hello() begin writeln('Hello world') end; begin hello() end.
In Pike syntax is similar to that of C and C++. The execution begins at main. The "argc" variable keeps the number of arguments passed to the program. The "argv" variable holds the value associated with the arguments passed to the program.
Example:
int main(int argc, array(string) argv)
In Python a function called main doesn't have any special significance. However, it is common practice to organize a program's main functionality in a function called main and call it with code similar to the following:
def main():if __name__=="__main__": main()
When a Python program is executed directly (as opposed to being imported from another program), the special global variable __name__ has the value "__main__".[1]
In REALbasic, there are two different project types, each with a different main entry point. Desktop (GUI) applications start with the App.Open event of the project's Application object. Console applications start with the App.Run event of the project's ConsoleApplication object. In both instances, the main function is automatically generated for you, and cannot be removed from your project.
In Ruby, there is no distinct main function. The code written without additional "class .. end", "module .. end" enclosures is executed directly, step by step, in context of special "main" object. This object can be referenced using:
self # => main
and contain the following properties:
self.class # => Object self.class.ancestors # => [Object, Kernel]
Methods defined without additional classes/modules are defined as private methods of "main" object, and, consequentally, as private methods of almost any other object in Ruby:
def foo 42 end foo # => 42 [].foo # => private method `foo' called for []:Array (NoMethodError) false.foo # => private method `foo' called for false:FalseClass (NoMethodError)
Number and values of command-line arguments can be determined using single ARGV constant array:
ARGV # => ["foo", "bar"] ARGV.size # => 2
Note that first element of ARGV array, ARGV[0], contains first command-line argument, not the name of program executed, as in C. Name of program is available using $0.[2]
In FMSLogo, the procedures when loaded do not execute. To make them execute, it is necessary to use this code:
to procname ... ; Startup commands (such as print [Welcome]) end
make "startup [procname]
Note that the variable startup is used for the startup list of actions, but the convention is that this calls another procedure that runs the actions. That procedure may be of any name.