views:

952

answers:

18

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)

+2  A: 

The language designers had to choose "some" name and main() sounds like the Main function, since that is where the execution starts :)

Amit
+8  A: 

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!)

Paul Tomblin
This is what I thought.
Kushal Paudyal
That would rise a new question: why did C use name "main"? =)
BalusC
I suspect that boiled down to 'they had to use something' and 'main sounds good'.
Michael Kohne
@BalusC: That's going to be a difficult question to answer (aside from "that's what it was in B"). "Why" questions on arbitrary decisions that aren't important in themselves (e.g., in the US it's not important that we drive on the right, only that we all drive on one side) are seldom fruitful.
David Thornley
@David: there is some evidence that the side of the road you drive on does have a slight effect on safety. This is because of the dominant eye of the people driving. http://en.wikipedia.org/wiki/Right-_and_left-hand_traffic#Safety_factors
rmeador
+2  A: 

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).

TLiebe
+1  A: 

Sometimes it's better not to change something just for the sake of changing it.

Chris Judge
+2  A: 

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.

Tenner
Actually, *you* can think of a better (highly subjective!) name: "where the main program flow starts" -> `program()`? `start()`?
Martinho Fernandes
It's actually not only a start, but the whole _main_ body of the program. You can add functions outside `main()`, but the _main_ flow of the program (at least in C and C++) is inside it.
Adriano Varoli Piazza
+9  A: 

How would you name the main function of a program?

rossoft
entry(), start(), execute(), run(), go(), ___(), $(), application(), ...
FractalizeR
`superAwesomeStartingPoint(final String[] args)` In the spirit of outrageously long names Java seems to like. `main` seems out of place in Java.
Ben S
And Microsoft's variants:my_entry(), my_start(), my_execute(), my_run() and my_go()
Bogdan Gusiev
@BenS, that is because it is from version 1.0, when they had things like getenv instead of getEnvironment. ;)
Yishai
In applets, java decided to go with init() instead of main (basically because main was taken already - but you see the point).
Yishai
@Bogdan Gusiev. Don't forget WinMain and WinMainW
Alexander Pogrebnyak
@Bogdan: they would probably be called EntryEx(), StartEx(), ExecuteEx(), RunEx(), GoEx(), instead. I don't recall any Microsoft API with a my_* naming convention.
Martinho Fernandes
I would name it "Bob". but then again, I enjoy naming things "Bob"...
Brian Postow
@Ben S: Java stole a bunch of stuff from C/C++. Guess what main is named in C! Answer: `main`, but it has a different return type.
R. Bemrose
@Brian Postow, +1 for the first truly worthy suggestion for a main replacement. ;-)
Bob Cross
I'd let you call it whatever you want as long as you give it an @EntryPoint annotation :)
Chris B.
+4  A: 

Quick answers:

  1. Why not?
  2. Why change it? To what?
  3. 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.... ;-)

Bob Cross
There are many good answers to the "To what?" question, some better some worse: `program()`, `start()`, `run()`...
Martinho Fernandes
@Martinho, no, those aren't better choices. "program" means a collection of source code. "start" and "run" are keywords that are used by Thread. Regardless, the fact that you could think of a new word doesn't answer the previous question: "why change it?" The answer would also have to be balanced against the reality that even if you could change "main" in Java (for example), you would break every Java program in the world. Are the aesthetics of a single name really worth it?
Bob Cross
@Bob you're arguing with hindsight. What's discussed is 'why did main() get to be named that way?', and it goes back at least 40 years, when there was no Java and no Thread.
Adriano Varoli Piazza
A: 

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...).

Adrien Plisson
+4  A: 

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.

chris
+7  A: 

Or, to be more obtuse, Why do we drive on the side of the road we do?

Answer: We had to choose something.

chris
actually I think that had to do with drawing swords and most people being right handed... Everyone used to walk/ride on the left, but then Napoleon changed it to make it more difficult to fight in the streets. At least that's the story I heard... I'm sure Snopes has an opinion.
Brian Postow
@Brian: When Sweden shifted from left to right back in 1967, the most popular argument for doing the switch was that it would be safer for the passenger to get out curb side (even when we were driving on the left, most vehicles (except for busses) had the steering wheel on left hand side).
Fredrik
+25  A: 

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.

Dr. Tim
Curious that it wasn't `enter()`, then, as the antonym of `exit()`.
caf
Perhaps it was the convention used when writing PDP-8 assembly language. See http://en.wikipedia.org/wiki/PDP-8 for a Hello World program using MAIN as the entry point.
Dr. Tim
+3  A: 

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.

Dan N
but if we renamed the main function FOO, then what would we call all of our temp little functions that aren't going to do anything useful???
Brian Postow
A: 

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

fortran
+1  A: 

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.

Jay
+20  A: 
Mark Rushakoff
+1 One of the very few answers with any real content.
Stephen Canon
Great answer and it incresed my knowledge too!
Xolve
gcc didn't exist when C was written.
Dr. Tim
superb answer ... :)
ravi
A: 

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).

Tim Schaeffer
+1  A: 

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.)

Debilski
+1  A: 

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?
DigitalRoss
If a girl knew what `int main(int argc, char ** argv){return 0;}` meant you think they would worry that it was called main?
mikek3332002