Verbs
Declare verbs, pass labelled arguments, return values with return, and give main an exit status.
A verb is Actus's word for a function: a named piece of code that you can run
from other places. You have used one in every program so far. main is a
verb. This page shows how to write your own.
Declare and call a verb
A verb declaration has the word verb, a name, a list of parameters in
parentheses, an optional return type after ->, and a body in braces.
There are two verbs besides main:
greethas no parameters and no return type. It does something and gives nothing back.addhas two parameters and returns anInt.
Each parameter has a role, a name and a type: abs left: Int. The role abs
means that add only reads the value. The next sections explain how the role
shows up in calls. A verb can be declared before or after the place that uses
it, so main can come first.
Calls use labels and roles
When you call a verb, you write the name of each parameter, a colon and the
argument: add(left: abs first, right: abs second). This is a labelled
argument. The label makes a call readable, because you see what each value is
for. The labels can come in any order:
An argument also repeats the role of its parameter. The parameter says abs,
and the call says abs first. Leave the role out and the compiler stops you:
The role at the call site is not decoration. It shows, right where the verb is called, how the value is used: here, only read. Ownership roles explains all four roles.
For an abs parameter you can also pass a number written in the call, as in
abs 10 in the first example, or a calculation in parentheses, like
abs (first + 1).
Using a label that the verb does not have is an error, and so is the wrong number of arguments:
Return a value
A verb that has a return type gives a value back with return. A return
can be anywhere in the body, so you can leave early:
sign returns at the first if that matches. pick returns the value of an
if, as you saw in Conditions and loops. say
has no return type, so it uses return; without a value to stop early.
A value is returned only with return. If the last line of the body is an
expression without return, the compiler explains what to do:
If the end of the body can be reached without a return, you get another
error:
The return value has a type, and the type is checked where the value is used:
You do not have to use a returned value. Calling square(n: abs five); on
a line of its own runs the verb and drops the result.
The other way round does not work. A verb without a return type, such as
greet, gives nothing back, so its call cannot be used as a value:
The same error appears when you pass such a call as an argument, use it in an
expression or return it. Write greet(); on a line of its own instead.
A verb that acts on a value
Some verbs are about one value: bump a counter, show it. Name that parameter
self. The call writes self like any other argument, with its role:
bump changes the counter, so main lends it with ins. show only reads
it, so main shows it with abs. The call tells the reader what each verb
does to the value, in the same way as for every other argument. There is one
form for a call. A call such as counter.bump() is an error (E1152) and the
message names the form to write instead:
main and the exit status
The program starts at main. It has no parameters. It can have no return
type, or it can return an Int, which becomes the exit status of the
program. By convention 0 means success and any other number means that
something went wrong.
The last line comes from actus run. It reports the status of your program.
A program that you built keeps the same status, and the shell shows it with
echo $?:
Without a return type, main exits with status 0. Exit statuses are one
byte on most systems, so use numbers from 0 to 255. A larger number is cut
down: returning 300 gives 44.
What you learned
verb name(role label: Type, ...) -> Type { ... }declares a verb.- Calls name every argument, and each argument carries its role:
add(left: abs first, right: abs second). - A parameter named
selfis called like any other:bump(self: ins counter). - A verb gives a value back only with
return. mainis where the program starts. ItsIntresult is the exit status.
Next: Structs, enums and case.