Some languages, such as C++ and Java, have grammar restrictions where code that is not a declaration or import statement cannot be outside of a function. However, variables, functions, and classes can be freely created at the 'top level'. Other statements are allowed inside functions, where the main function effectively acts as the top level.
This is in contrast to a language like Python which freely allows if statements, loops, and all other constructs to be outside of any functions in the top-level code environment.
What are the pros and/or cons of either approach? In which types of languages might it be useful to enforce declaration only top levels and when might it be alright to allow all kinds of statements at the top level?