main function

By convention, a function called main that holds a program's top-level steps and is called once at the end of the file.

In more detail

Putting the steps in main keeps the order of the program readable and avoids stray global variables. In Python and Bash it is only a convention, whereas languages such as C, Go and Java require a main function as the program's starting point. A Bash script calls it on the last line with main "$@", so nothing runs until the whole file has been read. Python programs usually call it from a main guard so that importing the file does not run it.

Example

def main():
    ...

main()

Related terms

Taught in

References