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

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?

1

u/[deleted] Aug 07 '25

g++ file.cpp

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

1

u/[deleted] Aug 07 '25

Correct. That's why the following doesn't make sense:

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

→ More replies