The main function

:main is a special and optional function.

Behavior#

  • If you define :main, the program executes it automatically when starting.
  • It can receive terminal arguments.
  • Only one main function can exist in the project.
  • Outside of main only function or class declarations, and global variables with static value can remain.
If there is loose code at the top level

If you call functions or execute statements outside of main, the program continues running, but main is no longer executed. The compiler assumes it is a direct script and that you don’t need that entry point.

Example with arguments#

When executing ./dinocode program.dino arg1 arg2, the arguments arrive at main:

name = "DinoCode 🦖"

:header
  print "--------------------------"
  print "Welcome to $name      "
  print "--------------------------"

:main args
  header
  print "Command line arguments:"
  for arg in args
    print arg

Script without main#

You define functions and call them at the end of the file.

:header
  print "--------------------------"
  print "Welcome to DinoCode 🦖  "
  print "--------------------------"

header
When to use main?

In small projects you can omit it. In programs that read console arguments or separate the startup phase well, main helps to order the flow.