Timeline for Why do we use main function in almost all the programming languages?
Current License: CC BY-SA 4.0
24 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
May 12, 2023 at 17:45 | comment | added | Mast | Many mature languages like Basic, Pascal or even Python don't have such requirements. It's mostly a thing in C-family languages, many of the others don't require it. Are C-family languages your background? | |
May 12, 2023 at 12:09 | comment | added | David Hammen |
@RBarryYoung void main() is a non-standard, implementation-defined interface from some company based in Redmond WA. The C and C++ standards both call for int main(void) or int main(int arc, char** argv) . main should return an int .
|
|
May 9, 2023 at 16:44 | comment | added | Pete Becker |
@RBarryYoung -- no, not at all. Each OS defines its own format for executable files, and that format defines how the entry point into the application is identified when the program is loaded and run. Compilers for high-level languages link to runtime code that has the OS-required entry point and supporting data. In C++ that runtime code typically does a bunch of initialization before calling the function named main .
|
|
May 9, 2023 at 16:15 | comment | added | RBarryYoung |
Effectively, { void main() } is the universal interface for an OS to call an executable program. Executable code without that interface isn't a program it's a library.
|
|
May 9, 2023 at 8:06 | comment | added | Stef |
OCaml and Python don't require a main function, yet if you read code written in these languages, you'll often find that developers choose to have a function called main which will be the starting point.
|
|
May 8, 2023 at 16:29 | answer | added | Sean Werkema | timeline score: 5 | |
May 8, 2023 at 15:52 | answer | added | ilkkachu | timeline score: 1 | |
May 8, 2023 at 15:05 | answer | added | Pablo H | timeline score: 1 | |
May 8, 2023 at 9:43 | answer | added | leo848 | timeline score: -1 | |
May 8, 2023 at 9:42 | comment | added | infinitezero |
You don't need a main function, you need an agreed starting point. It can be the first line of the file, it can be a function with a specific name (main ) or it can be whatever you like. Those are different conventions and different languages use different conventions.
|
|
May 8, 2023 at 8:46 | comment | added | Andrej Bauer |
For all intents and purposes __main__ is an environment, not a function. And also, stuff that is defined in class definitions is executed. Actually, everything is just exectuted.
|
|
May 8, 2023 at 5:52 | comment | added | kutschkem | @AndrejBauer That is not true, python does have a main function, it just hides it. If you execute a script, everything is implicitly in an "evironment" called "__main__", which is for all intents and purposes a function. docs.python.org/3/library/__main__.html | |
May 8, 2023 at 1:32 | comment | added | Ken Y-N |
@JeremyFriesner I believe you would still need an empty main() to satisfy the linker, though.
|
|
May 7, 2023 at 6:16 | comment | added | Peter Cordes |
@JeremyFriesner: True. You'd have no guarantee of which other static objects were already initialized and which weren't, but you could just avoid all other static constructors if you wanted to abuse the language this way. (And somehow convince a toolchain to link an executable without a main , perhaps with custom CRT startup code that's simpler.)
|
|
May 7, 2023 at 5:41 | comment | added | Jeremy Friesner |
I suppose that in C++ at least, you could (in principle) avoid the need for a main() function by instead declaring a static object and running all of your code from within that object's constructor. You wouldn't have access to argc and argv though, so you would either have to do without command line arguments, or find some other way to access them.
|
|
May 7, 2023 at 0:02 | answer | added | kaya3 | timeline score: 9 | |
May 6, 2023 at 23:54 | comment | added | Jörg W Mittag | What do you mean by "almost all the programming languages"? Of all the programming languages that I am using, none needs a main function. This includes, for example, Ruby, Python, Perl, PHP, ECMAScript, TypeScript, Smalltalk, Self, Newspeak, Scala, C#, Raku, Lua, Dart, Scheme, Racket, and many others. In this repository: github.com/JoergWMittag/lambdaconscarcdr I have implemented the same code in 65 different programming languages, and none of them require a main function. | |
May 6, 2023 at 21:46 | comment | added | einpoklum | I think this question belongs on StackOverflow rather than here. When considering programming abstractly, you can write whatever you like, e.g. write a function or construct an object and consider the complexity or other features of it. | |
May 6, 2023 at 20:02 | comment | added | Andrej Bauer |
OCaml and in generral the ML family of language do not have main . They just execute everything given to them. Python is also like that.
|
|
May 6, 2023 at 19:28 | history | became hot network question | |||
May 6, 2023 at 13:17 | answer | added | user16034 | timeline score: 10 | |
May 6, 2023 at 13:08 | answer | added | Rinkesh P | timeline score: 16 | |
S May 6, 2023 at 11:27 | review | First questions | |||
May 6, 2023 at 13:18 | |||||
S May 6, 2023 at 11:27 | history | asked | Kakashi San | CC BY-SA 4.0 |