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

-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

2

u/lkatz21 Aug 07 '25

That's not specifying "the entrypoint file", that's specifying the source files that make the program.

If you had two files, which would be the entry point?

1

u/[deleted] Aug 07 '25

You're right. I'm wrong.

1

u/GOKOP Aug 07 '25

That will only compile file.cpp. If you have any other files to compile you have to provide them too, otherwise they won't get compiled.

g++ file1.cpp file2.cpp

Which one is the "entrypoint file" now?

1

u/[deleted] Aug 07 '25

You're right. I guess it's just looking for the main function.

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

1

u/FarmboyJustice Aug 07 '25

I think you missed the part where this discussion is not about interpreted languages. This discussion. NOT the OP, but THIS discussion. The one you are in right now. Not every comment on every other comment.

1

u/[deleted] Aug 07 '25

It's definitely not though. Only one comment by Ikatz21 brought up compilers. Reread all of the comments in the chain.