]>
Book Contents Previous Section 2.2 Writing Types and Modes Next Section 2.4 Records Book Index

## 2.3 Declarations

A declaration is an expression used to restrict the type of values that can be assigned to variables. A colon ``:'' is always used after a variable or list of variables to be declared.

For a single variable, the syntax for declaration is

variableName : typeOrMode

For multiple variables, the syntax is

(, , ... ): typeOrMode

You can always combine a declaration with an assignment. When you do, it is equivalent to first giving a declaration statement, then giving an assignment. For more information on assignment, see Section ugIntroAssign and Section ugLangAssign . To see how to declare your own functions, see ugUserDeclare .

This declares one variable to have a type.

a : Integer
Type: Void

This declares several variables to have a type.

(b,c) : Integer
Type: Void

a , b and c can only hold integer values.

a := 45

45

Type: Integer

If a value cannot be converted to a declared type, an error message is displayed.

b := 4/5
Cannot convert right-hand side of assignment
4
-
5
to an object of the type Integer of the left-hand side.

This declares a variable with a mode.

n : Complex ?
Type: Void

This declares several variables with a mode.

(p,q,r) : Matrix Polynomial ?
Type: Void

This complex object has integer real and imaginary parts.

n := -36 + 9 * %i

-36+9 i

Type: Complex Integer

This complex object has fractional symbolic real and imaginary parts.

n := complex(4/(x + y),y/x)

4 y+x+y x i

Type: Complex Fraction Polynomial Integer

This matrix has entries that are polynomials with integer coefficients.

p := [ [1,2],[3,4],[5,6] ]

[1 2 3 4 5 6]

Type: Matrix Polynomial Integer

This matrix has a single entry that is a polynomial with rational number coefficients.

q := [ [x - 2/3] ]

[x-2 3]

Type: Matrix Polynomial Fraction Integer

This matrix has entries that are polynomials with complex integer coefficients.

r := [ [1-%i*x,7*y+4*%i] ]

[-i x+1 7 y+4 i]

Type: Matrix Polynomial Complex Integer

Note the difference between this and the next example. This is a complex object with polynomial real and imaginary parts.

f : COMPLEX POLY ? := (x + y*%i)**2

-y 2+x 2+2 x y i

Type: Complex Polynomial Integer

This is a polynomial with complex integer coefficients. The objects are convertible from one to the other. See ugTypesConvert for more information.

g : POLY COMPLEX ? := (x + y*%i)**2

-y 2+2 i x y+x 2

Type: Polynomial Complex Integer
Book Contents Previous Section 2.2 Writing Types and Modes Next Section 2.4 Records Book Index