Main function (programming)
From Wikipedia, the free encyclopedia
See also Point of entry regarding entry point.
In some programming languages, the main function is where a program starts execution.
It is the first user-written function run when a program starts (some system-specific software generally runs before the main function). 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 program's command-line arguments.
Contents |
[edit] C and C++
In C and C++, the function prototype of the main function is one of the following:
int main(void); 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) has a third argument giving the program's environment:
int main(int argc, char *argv[], char *envp[]);
The name main
is special; normally every C and C++ program must define precisely one function of that name.
[edit] C#
After executing a program written in C#, the environment looks up the root namespace, searches for the only class which has a static main method, and executes it.
public static void main(String[] args)
Command-line arguments are passed in args, similar to Java. But, unlike Java, C Sharp's main method may return with a value:
public static int main(String[] args)
[edit] Haskell
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.
[edit] Java
Java programs start executing at the main method, which has the following method heading:
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.
[edit] Pascal
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.
Example:
procedure hello() begin writeln('Hello world') end; begin hello() end.
[edit] Pike
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)
[edit] Python
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(): <main program> 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__".
[edit] REALbasic
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.