Skip to content
IsaacShelton edited this page Mar 21, 2022 · 1 revision

‘main’ function

The main function is the default entry point of the program

The verbose declaration of the main function is as follows:

func main(argc int, argv **ubyte) int {
    
}

Where

  • argc is the number of arguments supplied to the program
  • argv is a primitive array of null-terminated strings containing the supplied arguments
  • The return value of main will be the exit code of the program.

Concise Main Declarations

If argc and argv are unnecessary, then the argument list may be omitted:

func main int {

}

If the exit code is always zero, then the return type may be omitted:

func main(argc int, argv **ubyte) {

}

If the return type of the main function is void (either by explicitly using void or by omitting the return type), then the exit code will be zero.

If both the arguments and exit code are unnecessary, then both of them can be omitted to form the most concise form

func main {

}

Changing the Entry Point

By default, the function named main will be the entry point into the program. However, this can be changed by using the pragma entry_point directive before any function declarations.

pragma entry_point myEntryPoint

import basics

func myEntryPoint(argc int, argv **ubyte) int {
    printf("List of arguments:\n")
    
    each argument *ubyte in static [argv, argc] {
        printf("argv[%zu] = %s\n", idx, argument)
    }
    return 0
}
List of arguments:
argv[0] = ./test
argv[1] = a
argv[2] = b
argv[3] = c
argv[4] = 123
Clone this wiki locally