---
title: "1.3 The main function"
url: https://una-lang.org/en/learn/hello/the-main-function/
release: v0.4.0
edition: "0.2"
---

# 1.3 The main function

## 1.3.1 What `main` is

A program has many functions, and one of them is where a run starts: `main`. Think of the first step of a recipe: you may read the recipe in any order, but you cook it from step one. When you type `una run .`, `una` finds the module that `entry` names in `una.toml`, finds its function `main`, and follows the steps of `main` from the first to the last. When the last step ends, the program ends.

## 1.3.2 Why a program has one starting point

A module may declare many functions, in any order, as [section 1.2](https://una-lang.org/en/learn/hello/modules/#s1-2) shows. Without a fixed starting point, nobody could tell which of them runs first. With `main`, the answer is always the same: `una run .` starts at `main` of the entry module, and everything else runs because `main`, or a function it calls, asks for it.

## 1.3.3 How to write `main`

Here is a `main` with three steps:

:::steps
```una include="steps/08-greeting/src/greeting.una" title="src/greeting.una" verified="check, run"
module greeting

--- Greets the first customers of the day.
pub main() -> Unit
  uses @console: Console[print]
=
  @console.print("Good morning")
  @console.print("The store is open")
  @console.print("Today's special: desk lamps")
```

1. **`pub`.** Makes `main` **public**: reachable from outside its module, which is how `una run .` reaches it. [Chapter 6](https://una-lang.org/en/learn/functions/what-a-header-says-pub-private-functions-and-inferred-headers/#s6-3) covers `pub` in full.
2. **`main`.** The name, exactly `main`.
3. **`()`.** The parameters, the inputs of the function. `main` takes none, so the parentheses are empty.
4. **`-> Unit`.** What the function gives back when it ends. `Unit` means nothing useful: `main` runs for what it does, not for a value.
5. **`uses @console: Console[print]`.** What the function may do outside the program: print. [Section 1.5](https://una-lang.org/en/learn/hello/printing-the-console-print-and-uses/#s1-5) explains the line.
6. **`=`, then the body.** The **body** is the list of steps, each on its own line and indented by two spaces under `=`. The first lines, from `pub` to the `uses` line, are the function's **header**.
:::

The package's `una.toml` names the module `greeting` as its entry:

```toml include="steps/08-greeting/una.toml" title="una.toml"
[package]
name = "greeting"
version = "0.1.0"
edition = "0.2"

[run]
entry = "greeting"
grants = ["console"]
```

`una run .` prints:

```text include="steps/08-greeting/expected/output.txt"
Good morning
The store is open
Today's special: desk lamps
```

## 1.3.4 How `main` behaves

- **The steps run in order.** Each step runs to its end before the next one starts, from the top of the body to the bottom.
- **The program ends with `main`.** When the last step of `main` ends, the program ends.
- **`main` is found by its header.** `una run .` looks in the entry module for exactly `pub main() -> Unit`. A module without one may still check clean, since it may be meant for other modules to use, but `una run .` refuses to start it, and says that the module has no `pub main() -> Unit` to run. The same happens when `main` lacks `pub`.
- **The entry names the module.** `entry = "greeting"` names the module `greeting`, the file `src/greeting.una`. A package has one entry, and so one `main` that `una run .` starts.

## 1.3.5 When the program grows

`main` is where a run starts, not where all the work must be. As a program grows, its steps move into functions of their own, and `main` calls them in order, so that it reads like a table of contents: first the greeting, then the hours, then the offers. [Section 1.4](https://una-lang.org/en/learn/hello/writing-a-function/#s1-4) shows how to write such functions.

## 1.3.6 Check yourself: `main`

The sign on a store's front door is printed by this program:

```una include="steps/09-door-sign/src/sign.una#main" title="src/sign.una"
--- Prints the sign for the front door, with an empty line between its two parts.
pub main() -> Unit
  uses @console: Console[print]
=
  @console.print("Welcome")
  @console.print("")
  @console.print("Open from 9:00 to 18:00")
  @console.print("Closed on public holidays")
```

What does `una run .` print?

:::answer Show the answer
```text include="steps/09-door-sign/expected/output.txt"
Welcome

Open from 9:00 to 18:00
Closed on public holidays
```

Four lines, in the order of the four steps. The second step prints an empty text, `""`, which is a line with nothing on it.
:::
