aboutsummaryrefslogtreecommitdiff
path: root/doc/language/keyword/cpsc-keyword-declare.7.md
blob: 6860ff54a0136914031673a324c0a208eecf5a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

% CPSC-KEYWORD-DECLARE(7)

# NAME

cpsc-keyword-declare, cpsc-keyword-array, cpsc-keyword-integer, cpsc-keyword-real, cpsc-keyword-boolean, cpsc-keyword-char, cpsc-keyword-string - declare variables and arrays

# SYNOPSIS

```text
DECLARE name : type
DECLARE name : ARRAY[lower:upper, ...] OF type
```

# DESCRIPTION

Declare one variable per statement. Built-in types are INTEGER (signed 64-bit), REAL, BOOLEAN, and CHAR (one byte). STRING is an owning class supplied by the default standard library. User-defined TYPE and CLASS names are also accepted. Variables start with zero, FALSE, an empty string, or a zero-initialized user-defined value.

C_INT, C_CHAR, C_STRING and C_DOUBLE are native C aliases for int, char, char* and double. C_TYPE can introduce aliases for other supported C base types and pointers. C values start with zero or null; assignment from pseudocode types requires an explicit cast. See [cpsc-keyword-c_type(7)](cpsc-keyword-c_type.7.md).

ARRAY bounds are inclusive and may be expressions; every dimension needs an upper bound that is at least the lower bound (constant bounds that are inverted are a compile error, other bounds a run-time error), and an array too large to allocate is a run-time error; multiple ranges create multiple dimensions. Access elements with `name[index, ...]`. Bounds are checked at run time; an invalid index throws an exception that TRY/CATCH can handle. Whole-array assignment is unsupported. Declare arrays before using them; arrays declared inside a control-flow block stay in that block.

For class variables, an optional argument list after the type supplies constructor arguments, such as `DECLARE Item : Counter(3)`. Declarations zero storage and automatically construct class fields and array elements before calling the outer constructor. See [cpsc-keyword-class(7)](cpsc-keyword-class.7.md) for construction and NEW rebinding rules.

# EXAMPLE

```text
DECLARE Count : INTEGER
DECLARE Names : ARRAY[1:3] OF STRING
Count <- 2
Names[Count] <- "Alice"
OUTPUT Names[Count]
```

# SEE ALSO

[cpsc-language-overview(7)](../cpsc-language-overview.7.md), [cpsc-keyword-type(7)](cpsc-keyword-type.7.md), [cpsc-keyword-class(7)](cpsc-keyword-class.7.md), [cpsc-keyword-output(7)](cpsc-keyword-output.7.md), [cpsc-keyword-try(7)](cpsc-keyword-try.7.md).