Home > Back-end >  Why main() function is called at the end of the program in python?
Why main() function is called at the end of the program in python?

Time:10-15

Lets consider the following piece of code in Python:

def main():
    abc()
def abc():
      .
      .
      .
     <statements....>
      .
      .
      .
main() 
#Why the above line of code is used in python???

Lets consider other programming languages like C, C or Java or may be an interpreted language like BASIC. In BASIC, the code goes like this: if the name of the module is module1 then:

Module Module1
    Sub Main()
    Console.WriteLine("Hello World")
    Console.ReadKey()
    End Sub
End Module


This is in vbnet. No Main() is called. The above code is just for illustration. So why is main() called at the end of the program in Python?

CodePudding user response:

TL;DR

Because the creator of Python decided that was the way to use the language.


Details

Very simply put, it's a similar convention in those compiled languages you mentioned, like Visual Basic, C or C (or also Java, C#, and many other), that a program is started by launching the Main() function (with some variation, like in a module that is defined as the startup when you compile it).

So, basically, when you compile, in the binary .exe. the compiler adds a call to this Main() function, even if you haven't written it in your code.

Whereas in a Python program, it's simply the file itself that is run, line by line. There is no convention that any function would be called. Python is not a compiled program, not much happens "behind the scenes" (actually, not entirely true, some global variables are set by the interpreter Python.exe).

So, in your Python program, if there wasn't a line main() at the end, you would just have defined 2 functions abc and main, but nothing would be actually called and your program would stop after the definition, nothing inside the two functions would be executed.

In the end, it's just that Python language has a different rule than those languages you mentioned. It's by design of the language.

Of course, it's not "rnadom", this design is more "natural" depending on the type - compiled or interpreted - of the language. For instance, you have a similar behavior in JavaScript, there is no "main()" function either, a JavaScript code is also executed starting from the top, line by line, with potential functions defined in the main code.

(Simplified explanation, some special case may apply).

CodePudding user response:

Its because you have to define the function before calling it in Python. If you call it before the definition, it may give you an error. A VB program launches the Main() function automatically inside the startup module.

The general rule in Python is that a function should be defined before its usage, which does not necessarily mean it needs to be higher in the code.

lol()
def lol():
   print("Hello")

Should not work, but if you try

def test():
   lol()
def lol():
   print("Hello")

test()

It works.

  • Related