What is a main() function?

In most of the programming languages, there is this one special function i.e., main() which is executed firstly everytime when we run the program. The special function is nothing but the main function of the program i.e., main() function as we usually denote it in the code. The main() function of the program is the starting point of the program from where the code is start executing in most of the programming languages. A main() function must have specific return type while defining it in the program and it totally depends upon the requirements and functionality of that particular programming language standards.

main() function in Python

In Python, whenever we are writing a program, we do not need to define the main() function.This is because the Python interpreter runs the program from the very first line of it. The execution of a Python program goes line by line after starting from the first line. Therefore, it does not matter if the main() function is present in the program or not.

Since, we don’t have any main() function in our Python program, the interpreter will execute the code from 0 indentation When we give the command in Python to run the program.

Does a Python program need main() function?

Till now, in this tutorial, we have learned that Python interpreter execute the code from very first line and 0 indentation. So, we can conclude that we don’t have any compulsion to have a main function in our Python program. Nevertheless, if we have a specified or pre-defined starting point in our program is very useful to understand the functionality of the program. Before looking more into this, let’s look at the following example:

Example –

Output:

Explanation: In the above given code, we have first defined a default main() function in the program and not given any arguments as well as return type in it. After that, we have passed the print statement in the function. In last, after exiting the function, we have used a simple print statement to print a sentence in the output of the program.

As we can see in the output, only the sentence given in simple print statement i.e., “This is TutorialAndExample Website for Python tutorial!” is printed in the output and the print statement defined in the main() function is not printed. This happened because in the program we have not defined the call function i.e., “if__name__ == “__main__” in the program and therefore, the main() will not be called out by the interpreter and it will not print anything as result.

So, we have understood that defining a call function in the program for the main() function is very important and without defining it, we cannot execute the main() function from our Python program. Now, look at the following example, where we will define the call function:

Output:

Explanation: Now, we have defined the call function for the main function in the program. And because of this, the main() function will be executed by the interpreter. As we can see in the output, both the print statements have been printed the result in output once we give the command to run the program.

So,we have seen that how important call function is for using and executing the main() function. The call function becomes more important in Python because:

  • When we run the program, the Python interpreter will read the source file and execute all the code present in the file or program.
  • When we run the source file or program as main() function, the interpreter will set thespecial variable (__name__) to have a special value (“__main__”) in the program.
  • When we execute the main() function from our Python program, the interpreter needs an if statement to check if the __name__ is equal to __main__ or not.
  • The if__name__ == __main__ in Python also allow us to run the source file or program either as standalone program or reusable files.

The __main__ function

As we know that, to execute the main function, we have to define the __name__ == __main__ in the program as a call function. But what it actually does in the program?

If we are running the program or script directly in the Python, it will assign the __main__ to __name__ (It all happens in the background while we are writing the program). And this will end up, we are writing the conditional statement i.e., if__name__ == __main__ to check if the assignment is done or not. And, if Python evaluates this condition as to be true, then it means that the file is executed directly.

Let’s look at following program as one more example:

Output:

So, if we are running the program directly in the program, this condition is always going to be true as by default in the Python. Or in short, we can say that the if condition is used to check if we are running the Python script directly or importing it to execute the main() function in the program.

Now, let’s try to learn about this a little bit more by using the main() function as a module in the Python script or program.

Using __Main__ function as a module

When we use the main() function as a module by using a Python script, then the __name__ variable we defined in the program will get the name of the script we imported as value in it.

To import a Python script, first we have to create one. Write the following Python program in the Python shell:

Output:

We have tosave this program as a script file in the device in order to import in the other program.

Now, write down the following program in Python shell and also import the script file in it that we have saved:

Output:

As, we can see in the output that when we run the first file directly, the interpreter assigned the value of __name__ variable equals to __main__. But, when we run the second file by importing the first file in the program, then the interpreter assigned the value of __name__ variable equals to the name of the first file.

That’s how by the if statement, it is checked that if the main function of program is running directly on Python or a script file is imported in the program to execute the main() function.

Conclusion:

In this tutorial, we have learned about the main() function present in Python. We have also learned about that how we can execute the main function in the Python both by running directly the source file and by importing the script file in the program. We have also understood what is the difference between if condition execution both when the script file is imported and when the source file is run directly.

Pin It on Pinterest

Share This