Last Updated:

Procedures and Functions in Pascal

Programming is the process of step-by-step creation of a computer program, combining the preparation of its project, the use of a number of algorithms, the writing of program code, and so on. It involves the use of a peculiar language.

Procedures and Functions in Pascal

Types of subroutines

One example of such a language is the Pascal language, which has the means that provide the ability to design auxiliary algorithms as a separate, functionally independent, part of a program (subroutine). This feature is useful in the case of often found in the program subgoritms, as well as in the ability to apply some of the algorithms created earlier.

In addition to this, subroutines are used when dividing massive programs into small semantic components, according to a complex of independent modules, the composition and behavior of which obey certain programming rules (modular foundations).

A subprogram functions as a subprogram if:

  1. Identify the name of the subalhorhythm;
  2. Describe the algorithm based on Pascal's criteria.

If you want to call a subroutine, then indicate the name of the required subalgoritm on the required section, with a list of (input, output) information. The result is the implementation of the operators in the subprogram, as well as those executing the marked information.

Functioning will resume with an indication that follows simultaneously following the call of the subprogramme after the activity with the subprogram in use.

Types of Pascal subroutines:

  • procedures;
  • Functions.

The device of initial (input), as well as final (output) information is provided for mutual receipt of data between the blocks of the program, including procedures and functions.

Input parameters — the size translated into the subroutine from the calling block.

Output parameters are the size to be translated in the opposite direction.

Variables that take argument values (formal) are used when writing the text of a subroutine in the descriptions section. There is also an argument passed to the function when it is called (actual). You can call a single subroutine many times, working with different sets of inputs, but with one unchanged action.

Description. Calling Procedures and Functions


Usually, information about procedures and functions is located in the description item of the program. The design of the description of these types of subroutines is quite similar to the design of the Pascal program and includes:

  1. title;
  2. executable part (description of procedures);
  3. description section (descriptions of constants, types, labels, procedures, functions, variables).

The structure of a function in the Pascal programming language is represented in a certain way:

function function name (formal parameters): the type of the result;
the begin
descriptions section of the end function

The structure of the procedure in Pascal is shown as follows:

procedure, the name of the procedure (formal parameters);
The begin
procedure descriptions
section of the end procedure

Thus, variables are written that take the significance of the arguments in the title of procedures and functions (must be separated by the ";") sign:

var parameter name: type name

When specifying type names, you must use standard or identified by using the type command. The list of variables that take argument values is not required.

The procedure is called by the following operator:

Procedure name (list of actual parameters)

Sequential writing of arguments passed to a function when it is called, using commas, is a list of these parameters. During the call period, on the proper zones in the title, the formal parameters replace the actual ones.

Due to this process, the initial information is exchanged, and then the operators of the performed share of the procedure are performed. After the function is executed, the control returns back to the point in the program where the function was called (the calling block). The exchange of final information happens precisely during the process of activity of the executed part.

A similar method provides a function to implement a call from within any expression. The name of the function can be located in the right pane of the assignment operator, in the condition clause of the if statement, and so on.

Before returning, at the point of the main program where this function was called in the executable part, in order to provide summary information, you should place this command:

function name := result;

If there is a need to call a procedure and function, you should be guided by specific rules of relatively formal and actual parameters:

  1. The number of the two parameters does not have to differ in any way;
  2. Suitable characteristics must have a similar sequence regime and type.

The possible coincidence of the names of the actual and formal parameters does not provoke difficulties, due to the difference in the same parameters located in different parts of the memory system.

In addition, formal parameters are formed during the program call and disappear at exit, which makes these parameters only short-term variables.

Practical application of the procedure. The task is: to detect from several (the largest number is two) full-fledged numbers:

var x,y,m,n: integer;
procedure MaxNumber(a,b: integer; var max: integer);
if a>b then max:=a else max:=b;

write('Введите x,y ');

For a similar task, but with the use of functions, it is possible to find a solution as follows:

var x,y,m,n: integer;
function MaxNumber(a,b: integer): integer;
var max: integer;
if a>b then max:=a else max:=b;
MaxNumber := max;

write('Введите x,y ');
m := MaxNumber(x,y);
n := MaxNumber(2.x+y);