r/computerscience • • Aug 05 '25

Why do some programming languages have a "main" function and don't allow top-level statements?

Only language I've used with this design choice is C++ and while I didn't have much issues with it I still wonder why? Wouldn't that make the language more restrictive and difficult to use? What's the thought process behind making a language that requires a main function and not allowing any statements in the global scope?

40 Upvotes

74 comments sorted by

View all comments

Show parent comment

19

u/[deleted] Aug 05 '25

Typically in languages that allow top-level statements, execution starts at the top and goes to the bottom, so the entrypoint file (in Python, literally the __main__ module) is basically one big main function. import statements in Python happen in top to bottom order as well.

4

u/edgmnt_net Aug 06 '25

Yeah, although that doesn't really work well for compiled languages, unless you're willing to make the compiler interpret and run arbitrary code. Even if you do want to allow compile-time execution of certain things (for e.g. metaprogramming), there are more hygienic ways to do it. So this is only straightforward for interpreters because they can afford to not distinguish compile-time and run-time state/computations.

3

u/Hot-Profession4091 Aug 07 '25

Sure it does. C# is a compiled language and has top level statements. There’s just a lowering pass that generates the main method for you.

1

u/[deleted] Aug 07 '25

You don't need any compile-time execution to synthesize a main function from the interleaving of statements from top-level imports. Everything can still happen at runtime.

8

u/OpsikionThemed Aug 05 '25

Sure, that works fine; it's just not as intuitive (to me, at least) as having everything come from a single call stack.

5

u/[deleted] Aug 05 '25

CPython does in fact use a single callstack.

3

u/oneeyedziggy Aug 05 '25

How? The primary difference is whether you have 2 extra unnecessary lines, one to start kain and one to close it out... Why not leave them off and just use the start and end of the entrypoint file for the same purpose? 

11

u/OpsikionThemed Aug 05 '25

What do you mean, "entrypoint file"? 😉 Now we're talking about specially distinguishing parts of the code again.

2

u/el_extrano Aug 05 '25

FORTRAN (the '77 standard) is an example of a compiled language with top-level statements. Defining a main program was optional.

But if you had top-level statements in two files and try to link them together, you'd get an error. So to effectively have a main by declaring a MAIN explicitly, or just having one compilation unit with statements that aren't in a subroutine, which becomes the entry point.

0

u/brasticstack Aug 05 '25

It's the file you choose to run. That is your entrypoint (__main__ module in python terms.)  Nothing special about it except the that you chose to run it instead of some other file.

4

u/lkatz21 Aug 05 '25

When you compile the source you don't "choose to run" a file. All the files become one big file. So to so that you'd need to have a file designated in advance as the "main file" and the compiler would wrap the code in that file in the same main function.

-1

u/[deleted] Aug 05 '25

The difference between a compiled language and an interpreted one is orthogonal to the discussion of having an entrypoint function versus not having one.

4

u/lkatz21 Aug 05 '25

If you compile the source you need to have some way to specify the entry point. If it's not a function, it will be something else that is functionally equivalent, and would not be easier or less verbose than a main function

1

u/[deleted] Aug 07 '25

When compiling code you need to specify the entrypoint file already. Leaving off the main function declaration is purely less code. However, I was simply arguing that there are no technical difficulties with having no main function in a compiled language. I actually quite prefer a main function for its readability.

1

u/lkatz21 Aug 07 '25

When compiling code you need to specify the entrypoint file already.

Really? And how do you that?

→ More replies

0

u/[deleted] Aug 06 '25

This whole conversation is about compiled languages, and someone threw in python as a counter-example which is interpreted, therefore completely different. It is relevant and not "orthogonal".

1

u/[deleted] Aug 07 '25

Where in this thread did you pick up the idea that the discussion was about compiled languages only? There's nothing in the original post about that.

0

u/FarmboyJustice Aug 07 '25

A series of responses back and forth referencing a specific topic is a discussion. This particular discussion occurred within the context of a larger discussion prompted by a post.There is no law dictating that every comment in every discussion comply exactly with imaginary parameters assigned to the OP by random readers.

→ More replies

0

u/xenomachina Aug 05 '25

In Python, top-level statements are executed when the module they appear in is first loaded. In C and C++, modules aren't loaded at runtime. (At least, not normally.) So if you had a program that consisted of several modules, when would you expect the top-level code from each module to get executed?

-2

u/[deleted] Aug 05 '25

The answer is in my last sentence. imports are resolved in order from top to bottom, and they are deduplicated, so that subsequent imports of the same module do not re-run.

1

u/xenomachina Aug 05 '25

I know how it works in Python. I'm asking how it would work in C and C++ if they allowed top-level statements.

-1

u/[deleted] Aug 05 '25

I don't see why it would have to work any differently. It's just a matter of the compiler emitting a flag for whether a given module has been "imported," and then running the top-level code for that module upon first import.

2

u/xenomachina Aug 05 '25

and then running the top-level code for that module upon first import.

What is "first import" in C or C++?

0

u/[deleted] Aug 06 '25

If this feature were to be implemented, it would obviously be evaluated in lexical order, just like in Python or JS.

3

u/Miserable_Guess_1266 Aug 06 '25

What's the lexical order between multiple cpp files linked into the same executable or library? Or between multiple library linked into your executable?

This boils down to the same problem as the static initialization order fiasco.

→ More replies (2)