- Main function (programming)
In some
programming language s, 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 command arguments given to the program at the
command line interface .C and C++
In C and
C++ , thefunction prototype of the main function is one of the following:The parameters
argc
andargv
respectively give the number and value of the program'scommand-line argument s. The names ofargc
andargv
may be any valid identifier, but it is common convention to use these names. Other platform-dependent formats are also allowed by the C and C++ standards; for example,Unix (though notPOSIX.1 ) andMicrosoft Visual C++ have a third argument giving the program's environment, otherwise accessible throughgetenv
in
:stdlib.h Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary: [ [http://unixjunkie.blogspot.com/2006/02/char-apple-argument-vector.html Thechar *apple
Argument Vector] ]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) andEXIT_FAILURE
. The meaning of other possible return values is implementation-defined.By convention, the command-line arguments specified by
argc
andargv
include the name of the program as the first element; if a user types a command of "rm file
", the shell will initialise therm
process withargc = 2
andargv = ["rm", "file"]
. Asargv [0]
is the name that processes appear under inps
,top
etc., some programs, such asdaemon s or those running within an interpreter orvirtual machine (whereargv [0]
would be the name of the host executable), may choose to alter their argv to give a more descriptiveargv [0]
, usually by means of theexec
system call.The name
main
is special; normally every C and C++ program must define exactly one function with that name.main
must be declared as if it has external linkage; it cannot be declaredstatic
.
In C++,
main
must be in the globalnamespace (i.e.::main
) and cannot be a (class or instance)member function .Clean
Clean is a functional programming language based on graph rewriting. The initial node is called
Start
and is of type*World -> *World
if it "changes" the world or some fixed type if the program only prints the result after reducingStart
.Start :: *World -> *World Start world = startIO ...
Or even simpler
Start :: String Start = "Hello, world!"
One tells the compiler which option to use to generate the executable file.
C#
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 typestring []
, and has a return type ofvoid
orint
, and executes it [http://msdn.microsoft.com/msdnmag/issues/04/02/NETConsoleApps/] .Command-line arguments are passed in
args
, similar to how it is done in Java. For versions ofMain
returning an integer, similar to both C and C++, it is passed back to the environment as the exit status of the process.GNAT
Using
GNAT , the programmer is not required to write a function calledmain
; a source file containing a single subprogram can be compiled to an executable. The binder will however create a packageada_main
, which will contain and export a C-style main function.Haskell
In Haskell, there may be a
main
name bound to a value of typeIO ()
.IO
is a monad, which is used to separate side-effects frompurely functional areas of the program. Themain
value serves as the program's entry point.Command line arguments are not given to
main
; they must be fetched using another IO action, such as [http://haskell.org/ghc/docs/latest/html/libraries/base/System-Environment.html#v%3AgetArgsSystem.Environment.getArgs
] .Java
Java programs start executing at the main method, which has the following
method heading :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 theSystem.exit()
function.Unlike C, the name of the program is not included in
args
, because the name of the program is exactly the name of the class that contains the main method called, so it is already known.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.Perl
In
Perl , there is no main function. Statements are executed from top to bottom.Command-line arguments are available in the special array
@ARGV
. Unlike C,@ARGV
does not contain the name of the program, which is$0
.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)
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 calledmain
and call it with code similar to the following:When a Python program is executed directly (as opposed to being imported from another program), the special global variable__name__
has the value "__main__
". [ [http://www.artima.com/weblogs/viewpost.jsp?thread=4829 Pythonmain()
functions] ]Some programmers use the following, giving a better look to exits:
REALbasic
In
REALbasic , there are two different project types, each with a different main entry point. Desktop (GUI) applications start with theApp.Open
event of the project'sApplication
object. Console applications start with theApp.Run
event of the project'sConsoleApplication
object. In both instances, the main function is automatically generated for you, and cannot be removed from your project.Ruby
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:and contain the following properties:
Methods defined without additional classes/modules are defined as private methods of the "
main
" object, and, consequentally, as private methods of almost any other object in Ruby:Number and values of command-line arguments can be determined using the single
ARGV
constant array:Note that first element of
ARGV
,ARGV [0]
, contains the first command-line argument, not the name of program executed, as in C. The name of program is available using$0
. [ [http://www.ruby-doc.org/docs/ProgrammingRuby/html/rubyworld.html#UB Programming Ruby: The Pragmatic Programmer's Guide, Ruby and Its World] — on RubyARGV
]LOGO
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.AHLSL
In
AIGE 'sAHLSL , the main function, by default, is defined as:[main]
References
Wikimedia Foundation. 2010.