Home / Learn / 1 Hello, Una

Release v0.4.0Edition 0.2

1.3 The main function

Page 4 of 11

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 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:

src/greeting.unacheckrununa
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 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 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:

una.tomltoml
[package]
name = "greeting"
version = "0.1.0"
edition = "0.2"

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

una run . prints:

Output
Good morning
The store is open
Today's special: desk lamps

1.3.4 How main behaves

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 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:

src/sign.unauna
--- 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?

Show the answer
Output
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.

Next:1.4 Writing a function