# 1.2 Hello, Alice!

> Chapter 1.2: write, run, change and inspect your first Alice program.

Canonical: https://alice-lang.org/documentation/learn/first-program/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

Every language book starts by printing a greeting. It is a small program, but it proves that the compiler, the runtime and your editor all work.

## The smallest program

Make a directory for your experiments and create a file named `hello.al` in it:

```alice
puts "Hello, Alice!"
```

Run it:

```sh
alice run hello.al
```

```text title="Output"
Hello, Alice!
```

That is a complete program. There is no `main` function and no class around it. The statements at the top level of a file run from top to bottom, and together they are the program's entry point.

`puts` means "put string". It prints its argument and then a newline. The text in double quotes is a `String`.

## A program with a method

Now give the greeting a name. Try it in the browser first: press **Edit & run**, change `Alice` to your own name, and press **Run**.

```alice
def greet(name : String) : String
  "Hello, #{name}!"
end
puts greet("Alice")
```

Expected output:

```text
Hello, Alice!
```

[Edit and run in the Alice playground](https://playground.alice-lang.org/#code=u.ZGVmIGdyZWV0KG5hbWUgOiBTdHJpbmcpIDogU3RyaW5nCiAgIkhlbGxvLCAje25hbWV9ISIKZW5kCnB1dHMgZ3JlZXQoIkFsaWNlIik)

Let's read it line by line.

- `def greet(name : String) : String` starts a method called `greet`. It takes one parameter, `name`, of type `String`. The `: String` after the parentheses says that the method returns a `String`.
- `"Hello, #{name}!"` is string interpolation. Alice evaluates the code between `#{` and `}` and puts the result into the string.
- There is no `return`. The value of the last expression in a method is its result.
- `end` closes the method.
- `puts greet("Alice")` calls the method and prints what it returns.

The same program as a file runs the same way:

```alice
def greet(name : String) : String
  "Hello, #{name}!"
end

puts greet("Alice")
```

```text title="Output"
Hello, Alice!
```

## What the compiler checks

The parameter type is a promise. Change the call to `greet(42)` and run it again. The program does not start. The compiler tells you what is wrong and where:

```alice fail="AL0305"
def greet(name : String) : String
  "Hello, #{name}!"
end

puts greet(42)
```

```text title="Error"
error[AL0305]: argument 1 of greet: expected String, got Int32
--> main.al:5:1
```

Every Alice error has a code like `AL0305`. [Appendix B](https://alice-lang.org/documentation/learn/error-codes.md) lists them all. The line starting with `-->` gives the file, the line and the column.

## Look under the hood

`alice run` does several things at once. You can also do them one by one:

```sh
alice check hello.al   # report errors, do not run
alice build hello.al   # write a .NET assembly, do not run
alice emit hello.al    # print the C# the compiler generates
```

The compiler turns Alice into C#, and the .NET compiler turns that into IL, the same bytecode C# produces. `alice emit` is a way to learn what your code becomes. It is for reading only: keep your `.al` file as the source.

`alice run` caches the compiled program. The first run of a new file takes about a second. Running the same file again starts in about a tenth of a second.

:::caution\[Common mistakes]

- **Single quotes for text.** `'Hello'` is a string without interpolation, as in Ruby, so `'#{name}'` prints the braces literally. Use double quotes when you interpolate.
- **Forgetting `end`.** Every `def`, `if`, `while` and `do` block needs an `end`. Without it you get `error[AL0101]: expected 'end'`.
- **A `return` type that does not match.** If a method says `: String`, its last expression must be a `String`.
  :::

## Summary

- A file of top-level statements is a complete program. `alice run file.al` compiles and runs it.
- `def name(param : Type) : Type ... end` defines a method. Its last expression is its result.
- `"#{expr}"` interpolates a value into a string.
- Type errors stop the build before the program runs. Each error has an `AL` code.
- `alice check`, `alice build` and `alice emit` run the steps of `alice run` one at a time.

The exercises for chapter 1 are at the end of [1.3](https://alice-lang.org/documentation/learn/first-project.md#exercises).

Next, turn the file into a [project](https://alice-lang.org/documentation/learn/first-project.md).

## Continue reading

- Previous: [1.1 Installation](https://alice-lang.org/documentation/learn/installation.md)
- Next: [1.3 Hello, project](https://alice-lang.org/documentation/learn/first-project.md)
