November 22, 2020•3 min read

User-Defined Data Types in Pascal

User-defined data types in Pascal come in 2 kinds, Enumerated and Subrange, and you declare both of them yourself.

Read in Indonesian

Last time we covered the standard data types in Pascal. Now we'll make our own, which people usually call user-defined data types.

📷 Illustration: a hierarchy chart of data type classification in Pascal, from standard types and programmer-defined types through pointers to structured types.

In Pascal's family of data types, user-defined types (user defined data types) generally split into 2 main kinds, Enumerated and Subrange. So what are Enumerated and Subrange? let's look at them more closely...

Enumerated Types

An enumerated type lets the programmer declare the set of valid values for that type. A simple example is a type for day names, holding senin, selasa, rabu, and so on.

Enumerated types are often just called enum.

The declaration format is as follows.

TEXT
type
  enum-name = (item1, item2, item3, ...)

Here is an example of using enum to build a data type for day names and month names.

PASCAL
program enum;
uses crt;
type
  NamaHari = (senin, selasa, rabu, kamis, jumat, sabtu, minggu);
  NamaBulan = (januari, februari, maret, april, mei, juni, juli);
var
  hari: NamaHari; // variable hari has type NamaHari
  bulan: NamaBulan; // variable bulan has type NamaBulan
begin
  clrscr;
  hari := senin;
  bulan := juli;
  writeln(hari, ' ', bulan);
  readln;
end.

In that example we create a new data type, an enum, by writing it below the type keyword. The first enum is NamaHari, holding day names from senin through minggu. The next one is NamaBulan, holding month names from januari through juli.

The next step is to use the enum we just made as the type of a variable. The first variable is hari, with the type NamaHari, and the second is bulan, with the type NamaBulan.

Both variables can only be filled according to what we declared up front. Say you assign the value 'desember' to the variable bulan: the program errors with Identifier not found "desember", because 'desember' is not in the declaration.

Oh, right: when you fill a variable of an enumerated type, you don't use quote marks, because the value is already declared. It works the same way as assigning true or false to a boolean data type (you don't write 'true' or 'false').

Subrange Types

A subrange is a user-defined type that holds a particular range of another data type. An example is an exam score, which ranges from 0 to 100. Another is a grade label, which ranges from 'A' to 'E'.

You can write a subrange directly as the data type of a variable, or below the type keyword.

TEXT
type
  subrange-name = lower-bound...upper-bound;
// or
var
  variable-name: lower-bound...upper-bound;

An example of using a subrange for exam scores and grade labels.

PASCAL
program subrange;
uses crt;
type
  RentangNilai = 0..100;
  RentangPredikat = 'A'..'E';
var
  nilai: RentangNilai;
  predikat: RentangPredikat;
begin
  clrscr;
  nilai := 78;
  predikat := 'C';
  writeln(nilai, ' ', predikat);
  readln;
end.

In that example we create two new types: RentangNilai, which holds numbers from 0 to 100, and RentangPredikat, which holds the letters 'A' to 'E'.

Then we declare the variable nilai with the type RentangNilai, and predikat with the type RentangPredikat. Both can only be filled with the limited values from the declaration. If you try to put 200 into the nilai variable, the program errors out.

That code can actually be simplified further, like this.

PASCAL
program subrange;
uses crt;
var
  nilai: 0..100;
  predikat: 'A'..'E';
begin
  clrscr;
  nilai := 78;
  predikat := 'C';
  writeln(nilai, ' ', predikat);
  readln;
end.

In that version you only write the range as the data type, without declaring it separately. It looks cleaner, doesn't it, hehe...

Wrapping Up

That's enough for this tutorial on user-defined data types in Pascal, hope it helps :D next time we'll cover one of the structured data types, namely RECORD in Pascal.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.