Previous articles from Mytour introduced readers to operators in Pascal as well as constants and how to declare them in Pascal. This article continues the discussion by introducing procedures in Pascal.
Defining Procedures in Pascal
Defining procedures in Pascal involves using the Procedure keyword. The general structure of a procedure definition is as follows:
procedure name(arguments): type1, (arguments): type 2, ... );
< local='' declarations=''>
begin
< procedure='' body=''>
end;
Defining a procedure in Pascal involves a header, local declarations, and a part of the procedure. The procedure header includes the procedure keyword and the name assigned to the procedure. Below are the parts of a procedure in Pascal:
- Argument: Parameters establish a link between the calling program and the procedure identifiers, also known as formal parameters. The rules for parameters in procedures are similar to those for functions.
- Local declaration: Local declaration refers to declarations for labels, constants, variables, functions, and procedures, applicable only to the Procedure Body.
- Procedure Body: The Procedure Body contains a set of commands defining the procedure. It always accompanies the words Begin and End, and is a part of the procedure where operations are executed.
Below is the source code of a procedure named findMin(). This procedure includes 4 parameters x, y, z, and m, and stores the minimum of the first 3 variables in the variable m. Variable m is passed by reference.
Declaring procedures in Pascal
Declaring procedures in Pascal serves to inform the compiler about the procedure's name and how to call the procedure. The components of a procedure are defined separately.
The syntax for declaring a procedure is as follows:
procedure name(arguments): type1, (arguments): type 2, ... );
Note that the name of the procedure is independent of anything else. For example, with the definition of the findMin() procedure above, here is the syntax for declaring the procedure:
procedure findMin(x, y, z: integer; var m: integer);
Calling procedures in Pascal
During the creation of a procedure, you must provide a definition of what the procedure should do. To use a procedure, you must call that procedure to perform the designated tasks.
When a program calls a procedure, control of the program is transferred to the called procedure. A called procedure executes the specified tasks, and when it reaches the End statement, it returns control back to the calling program.
To call a procedure in Pascal, simply provide the required parameters along with the procedure name as shown below:
When the above code is compiled and executed, it will return the following result:
Recursive subroutine in Pascal
A program or subroutine can call other subroutines. When a subroutine calls itself, it is called a recursive subroutine.
To gain a better understanding of recursive subroutines in Pascal, refer to the example below, which calculates the factorial of a given number, n:
The following program calculates the factorial of a given number by recursively calling itself (also known as the called program being a recursive subroutine):
When the above code is compiled and executed, it will return the following result:
Here's another example, creating a Fibonacci Series for a specific number using the recursive function:
When the above code is compiled and executed, it will return the following result:
Parameters of a subroutine
If a subroutine (function or procedure) uses parameters, it must declare variables to accept the values of the parameters. These variables are called formal parameters of the subroutine.
Formal parameters act like local variables within the subroutine and are created when entering the subroutine and destroyed when exiting the subroutine.
When calling a subroutine, there are 2 ways to pass arguments to the subroutine.
By default, Pascal uses call by value to pass arguments. This means that the code within the subroutine cannot change the arguments used to call the subroutines. For example, the max() function uses call by value.
The programs (exProcedure) call the findMin() procedure above using call by reference.
We hope after reading this article, readers will gain more information about procedures in Pascal. Additionally, to understand more about operators in Pascal as well as the advantages and disadvantages of Pascal, readers can refer to some other articles on Mytour.