Skip to content
Adam
Language

Declarations

Constants, variables, routines and their parameters. Every name is declared before it is used.

Examples: res\examples\language\declarations holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.

Constants and variables

A constant always names its type (LIMIT: int64 = 3;): no type is taken from the value. A module-level variable starts at its type's zero.

declarations_basics.adam
module exe declarations_basics;

const
  LIMIT: int64 = 3;

var
  GTotal: int64;

// AValue doubled
func Twice(const AValue: int64): int64;
begin
  return AValue * 2;
end;

// The caller's own variable, grown by one
func Bump(var AValue: int64);
begin
  AValue := AValue + 1;
end;

begin
  println("starts at {}", GTotal);
  Bump(GTotal);
  GTotal := Twice(GTotal) + LIMIT;
  println("now {}", GTotal);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
starts at 0
now 5

A threadstatic block, at module level only, declares variables with one copy per thread, each starting at its initializer or zero.

Parameters

  • A parameter with no mode is passed by value.
  • A const parameter is read only.
  • A var parameter is the caller's own variable.
  • A literal parameter takes only a constant expression.

A call passes exactly a routine's parameters: there are no parameter defaults. A trailing ... makes a routine variadic, its arguments read through varargs.

Forward and overloads

A name is declared before it is used, and forward declares a routine or a type ahead of its body, so two routines can call each other. Routines of one name in one scope are overloads when their parameter types differ in count or in a type; parameter names, var / const, linkage and the result type make no overload.

declarations_forward.adam
module exe declarations_forward;

forward func IsOdd(const AValue: int64): bool;

// True when AValue is even
func IsEven(const AValue: int64): bool;
begin
  if AValue = 0 then
    return true;
  end;
  return IsOdd(AValue - 1);
end;

// True when AValue is odd
func IsOdd(const AValue: int64): bool;
begin
  if AValue = 0 then
    return false;
  end;
  return IsEven(AValue - 1);
end;

// An integer in words
func Show(const AValue: int64): string;
begin
  return FormatStr("int64 {}", AValue);
end;

// A string in words
func Show(const AValue: string): string;
begin
  return FormatStr("string {}", AValue);
end;

begin
  println("7 is odd: {}", IsOdd(7));
  println("{}", Show(42));
  println("{}", Show("hi"));
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
7 is odd: true
int64 42
string hi

Linkage

A routine's linkage word, clink (the default) or cpplink, names its symbol as C or as C++ does: clink and cpplink.

noreturn marks a routine that never returns to its caller (func Halt(const ACode: int32); noreturn;); a call of one ends the path as raise does.

Inline routines

inline copies a routine's body into every call, at every @optimize level, debug included: no call is made and no frame (func Square(const AX: int64): int64; inline;), and stepping into a call lands on the routine's own lines. An inline routine may call other inline routines, take var parameters and be generic. It cannot be external, variadic or a generator, cannot be named as a value, and cannot reach itself through calls of inline routines. A unit's public inline routine may use the unit's private declarations.

declarations_inline.adam
module exe declarations_inline;

// Copied into each call: no call is made
func Square(const AValue: int64): int64; inline;
begin
  return AValue * AValue;
end;

// An inline routine calling another: both bodies end up in the caller
func FourthPower(const AValue: int64): int64; inline;
begin
  return Square(Square(AValue));
end;

// A var parameter works as in any routine
func Bump(var ACount: int64); inline;
begin
  ACount := ACount + 1;
end;

// A generic inline routine: each instance is copied into its calls
func Clamp<T>(const AValue: T; const ALow: T; const AHigh: T): T; inline;
begin
  if AValue < ALow then
    return ALow;
  end;
  if AValue > AHigh then
    return AHigh;
  end;
  return AValue;
end;

var
  GCount: int64;
  GTotal: int64;
  GI: int64;

begin
  println("9 squared is {}", Square(9));
  println("fourth power of 3 is {}", FourthPower(3));
  Bump(GCount);
  Bump(GCount);
  Bump(GCount);
  println("counter is {}", GCount);
  println("clamped: {} {} {}", Clamp<int64>(-4, 0, 9), Clamp<int64>(5, 0, 9),
    Clamp<int64>(12, 0, 9));
  for GI := 1 to 4 do
    GTotal := GTotal + Square(GI);
  end;
  println("sum of squares 1..4 is {}", GTotal);
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
9 squared is 81
fourth power of 3 is 81
counter is 3
clamped: 0 5 9
sum of squares 1..4 is 30
↑ ↓ moveEnter openEsc close