Where is the main function in EK9?

← Getting Started · Ref: Q17

EK9 does not have a main() function. Instead, you define named programs inside a defines program block.

A program is a named method inside defines program:

  defines program
    MyProgram()
      stdout <- Stdout()
      stdout.println("Hello")

Key points:

1. Programs are NAMED, not anonymous main functions

   You choose the name: HelloWorld(), DataProcessor(), WebServer(). This is more descriptive than a generic main() entry point.

2. Multiple programs can exist in one file

   Related utilities can live together in the same module. Run a specific one with: ek9 -r ProgramName file.ek9. If a file has only one program, -r is optional.

3. Programs can accept typed command-line arguments

   Declare parameters with -> and EK9 automatically converts string arguments to the declared types (String, Integer, Float, Boolean, Date, Time, and more). Exit code 9 means wrong argument count. Exit code 10 means type conversion failed.

4. Programs share module resources

   All programs in a module can access the module's functions, classes, constants, and types.

The code example below shows two programs in one module: a simple greeting and one that accepts a name argument.

See Q1 for a minimal program. See Q5 for source file structure. See Q49 for defining functions that programs can call. See Q93 for defining classes. See Q111 for components.

Example

defines module qa.getting.started.entry.point

  defines function

    greetingMessage()
      <- message as String: "Welcome to EK9"

  defines program

    SimpleGreeting()
      stdout <- Stdout()
      stdout.println(greetingMessage())

    PersonalGreeting()
      -> name as String
      stdout <- Stdout()
      stdout.println(`Hello, ${name}!`)

Common mistakes

E50001 — A typo in the function name 'greetingMessge' means the compiler cannot find any identifier with that name. Check spelling carefully as EK9 is case-sensitive. See ek9 -h E50001 for details.

Incorrect:

stdout.println(greetingMessge())

Correct:

stdout.println(greetingMessage())
Other ways to ask this
  • What is the entry point in EK9?
  • How do I define a program in EK9?
  • Can I have multiple programs in one file?
  • How does defines program work?
  • How do I pass arguments to a program?
  • How do I accept command-line parameters?

Coming from another language?

C: main(), Java: public static void main(String[] args), Go: func main() in package main, Rust: fn main(), Python: if __name__ == '__main__'. EK9 uses named programs in defines program blocks. Unlike Java and Go, multiple entry points per file. Unlike C and Rust, programs are named and selectable at runtime.

Keywords: main, first, parameters, beginner, intro, program, entry, run, start, multiple, arguments, function, defines, point