Why the function name main() is retained in many languages like C, C++, Java? Why not any other names for that function? Is there any common structure for all these 3 main() (in C, C++, Java)
The language designers had to choose "some" name and main() sounds like the Main function, since that is where the execution starts :)
Because C did it, C++ retained it to be compatible, and Java did it to ease the transition from C++. Back in the early days of Java, employers often hired people who had C++ experience because it was so similar. Not like today where they want the new guy to have more Java experience than Gosling.
And lets not forgot that PL/1 used "procedure options main" for the same purpose. (Man, that's refreshing some memory cells that haven't been touched in a while!)
Probably because it's the main function that has to run. C++ inherited the name from C and Java inherited it from C++ (programmers don't like change).
Sometimes it's better not to change something just for the sake of changing it.
You've got to name it something. And I can't think of any better name, since that's where the main program flow starts.
There is no common structure, except maybe the ability to take arguments. Nor should there be a common structure, since the whole point of a program is to do whatever the programmer wants. I.e., anything.
Quick answers:
- Why not?
- Why change it? To what?
- Because it's one of the symptoms that C, C++ and Java all share a common ancestry (specifically, that C has heavily influenced the other two). You won't see main() in Scheme or Prolog, for instance.
Personally, I think the answer to questions 2a and 2b are the most important. If you really want to break every C/C++/Java program in the world in order to repair what you feel are flawed aesthetics of a single function name, I would have to ask you if you have your priorities in order.... ;-)
because C, C++ and Java needed a way to know where the main function is...
however, many other languages allow it to be named the way you like and have a way to tell the compiler which function is the entry-point (compiler option, pragma, special statement...).
It's not always main().
Java Applets use init() and start() for the external caller to hook into.
Servlets are started via init() and service() methods.
(service will dispatch to the more familiar doGet and doPost methods)
Granted, these exceptions do rely on some container other than the OS to invoke the methods.
Or, to be more obtuse, Why do we drive on the side of the road we do?
Answer: We had to choose something.
There are a lot of silly and not very respectful answers here to a legitimate question.
C didn't come from nowhere. Its immediate ancestor is B, written by Ken Thompson. Here is a link to the B manual. The basic structure of a B program is
main(); exit();
main() is provided by the programmer and exit() is supplied by the library. This seems to be the first appearance of main() as the predecessor of B, BCPL, has no such concept. I guess you would have to ask Ken Thompson why it was main and not something else.
Because it is called main function. The term main function had been used at least since 1960. At PL/I, the function which started the execution had the following header.
FOO: PROCEDURE OPTIONS(MAIN);
where FOO is the function name.
Because it is as good as any other. The other way to go is to use an unnamed block, as in Pascal and its derivatives, or in most of the scripting languages, where the "main function" (if allowed to call it so) is just the beginning of the file. Then, you have to care from where you get the program arguments (some library or global variable) and you can ask why they have chosen args
instead of arguments
or argv
or param
, etc.
Sincerely, I never thought somebody would care about that almost irrelevant conventionalism xD
Well, it either had to have a fixed name, or you would have to give the programmer a way to specify the name.
If the programmer could pick the name, then there would have to be an extra statement or feature of some kind in the language just to handle that. And what would be gained? Arguably we'd be worse off: Then when you wanted to find this function, you'd first have to look for the thing that says what it's called, then you'd have to look for the function itself, so there would be two steps instead of one.
Given that it will have a fixed name, somebody had to pick what that name would be. One could think of many candidates: "start", "run", whatever. I doubt there was any overriding reason why "main" was chosen. Somebody had to pick something, that was as good a choice as any.
If you develop an embedded system, you may see other names.
ECos uses
cyg_user_start().
(or main(), depending on the setup).
A linux kernel module uses a function marked with __init (though that's not the same thing, since modules are event-driven, typically).
Unfortunately, I’m not able (yet) to directly comment, so I’ll have to give a full answer without knowing an answer to your question at all.
But, however, I’d like to point out to all the people saying ‘what other than main() should it have become anyway?’ that indeed there is no need for a named function at all. It could well have been {}
with the convention that code inside these anonymous brackets is the main function and that’s it. (So it’s not only implying int
when the return type is missing but also implying so to say main()
when the function name is missing.)
More interesting questions would be...
You got so close to some questions that would almost be interesting...
- is
main
part of the language or the library? - how does my program get started?
- does anything run before
main()
? - what was the first language to use
main()
? - can I write a program that uses no libraries? Does it need a
main()
? - what happens when I return from main()?
- to impress girls, how could I change the name from
main()
to something more cool?