April 15, 2021•3 min read

Using and Writing a PROCEDURE in Pascal

The main parts of a PROCEDURE are its name and its body, with parameters and local declarations as the optional parts.

Read in Indonesian

After covering procedural programming in Pascal, this time we'll go further into using and writing a PROCEDURE.

Declaring and Structuring a PROCEDURE

A PROCEDURE is declared right before the BEGIN part of the main program, and after the variable declarations.

As a sub program, a PROCEDURE has components that are roughly the same as the main program.

The main parts of a PROCEDURE are its name and its body. There are also optional parts, namely parameters and local declarations.

TEXT
procedure <procedure-name>(<parameter>: <data-type>);
// local declarations
begin
  // procedure body
end;

Here is an example of a PROCEDURE that prints the star character n times.

PASCAL
procedure cetakBintang(n: integer);
var
  i: integer;
begin
  for i:=1 to n do
    write('*');
end;

We'll cover using that PROCEDURE later. For now let's go through its parts first.

1. The PROCEDURE Name

The PROCEDURE name is what we use to identify and call the PROCEDURE.

It sits after the procedure keyword, followed by an opening parenthesis if there are parameter declarations. As far as possible, a PROCEDURE name should describe its specific purpose.

As in the example above, since the job is to print the star character n times, the PROCEDURE is named cetakBintang.

2. Parameters

A parameter is a variable that can be filled in from the main program. The job of a parameter is to control the output of that PROCEDURE.

Parameters are written inside the parentheses after the PROCEDURE name.

Since a parameter is a variable, you write it the same way as a variable declaration.

In the earlier example we declared one parameter named n with the integer type.

If you want to make several parameters in one PROCEDURE, you can separate them with commas. Like this.

PASCAL
procedure contoh(a, b: integer, c: string);
begin
  // procedure body
end;

If you want a PROCEDURE with no parameters, the parentheses don't need to be written at all.

PASCAL
procedure contoh;
begin
  // procedure body
end;

3. Local Declarations

Local declarations work like the main program declarations. In this local part we can declare variables, constants, and types.

In the earlier example we made a local variable declaration named i with the integer data type.

That variable i only has scope inside this PROCEDURE. If you try to use it in the main program, an error occurs.

4. The PROCEDURE Body

The last part, and the most important one in a PROCEDURE, is the body. Here you can write the program logic the same way you write it in the main program.

The PROCEDURE body starts with the begin keyword and ends with end; (using a semicolon, not a period).

Using a PROCEDURE in Pascal

Now that you know the parts of a PROCEDURE, let's build a complete program using the PROCEDURE we made earlier.

Type the following code into Free Pascal.

PASCAL
program prosedural;
uses crt;

procedure cetakBintang(n: integer);
var
  i: integer;
begin
  writeln('mencetak * sebanyak ', n, ' kali');
  for i:=0 to n do
    write('*');
end;

begin
  clrscr;
  cetakBintang(10);
  readln;
end. 

Now run it, and the output that appears will look roughly like this.

TEXT
mencetak * sebanyak 10 kali
**********

What happened?

First let's focus on the main program part.

PASCAL
...
begin
  clrscr;
  cetakBintang(10);
  readln;
end. 

In the main program we call the PROCEDURE cetakBintang by typing the PROCEDURE name followed by the parameter value inside the parentheses.

Pascal then looks for the PROCEDURE with that name. Because we pass the value 10 (the one inside the parentheses), the variable n in cetakBintang now holds 10.

PASCAL
...
// cetakBintang(10) -> the value of n is now 10
procedure cetakBintang(n: integer);
var
  i: integer;
begin
  writeln('mencetak * sebanyak ', n, ' kali');
  for i:=0 to n do
    write('*');
end;
...

The program then runs the PROCEDURE body according to the statements we wrote. Since the value of variable n is 10, the program logic prints the star character 10 times.

Try changing the parameter value when you call cetakBintang, and the number of star characters printed will change too.

Wrapping Up

That's all for this time, hope it helps :D

Next time we'll cover one more kind of sub program, namely writing and using FUNCTION in Pascal.

See ya...

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
PascalPascal Units: One Average, Two Answers

The same two scores print 7 in one program and 7.50 in another, because each copy of the function divides differently. A unit gives it one body, and the interface decides who may call it.

For developers who learn by struggling

© 2026 Inva.dev.