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)
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Multiple sockets for clients to connect to
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
More interesting questions would be...
You got so close to some questions that would almost be interesting...
main
part of the language or the library?main()
?main()
?main()
?main()
to something more cool?Because it is main function. The term main function has been used at least since the 1960s. In PL/I, the function which started the execution had the following header:
where
FOO
is the function name.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.
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).
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 ofarguments
orargv
orparam
, etc.Sincerely, I never thought somebody would care about that almost irrelevant conventionalism xD