Last Updated:

Procedures and functions - Pascal algorithmic language

Almost all algorithmic languages have the ability to program functions and procedures - blocks of operators, designed in the form of subroutines. The development of functions and procedures is necessary when repeatedly used in different places of the program or in several programs of a block of operators that perform the same type of actions, for example, calculating the values of a complex function at different values of the argument. In Turbo Pascal it is also possible to create libraries (modules) consisting of special procedures and functions different from those supplied in the package (System, Crt, Graph modules).


Procedures (subroutines) and functions defined by the programmer are given in the description section of the main program. Procedures and functions have a title, a description section, and an operators section.


The procedure header consists of the service word Procedure, the name of the procedure, and a list of parameters,

for example:


The function header consists of the function service word, the function name, and a list of parameters, and the type of value returned by the function is also indicated,

for example: 

Here:
Function and Procedure are service words,
Name_F, Name_P are the names of the function and procedure, respectively,
p1, p2 are the names of formal parameters-values,
p3, p4 are the names of formal parameters-variables,
. . . - the ellipsis means the possibility of enumerating more parameters.

In the future, if not specifically stipulated, everything said about the procedure also applies to the function.


The type of value returned by the function can be simple, string, or pointer type. The type of formal parameters can be anything, but must be specified only by the identifier (type name). Thus, the type name of the formal parameter - array must be specified in advance in the Type statement, for example: Type M= array[1..100] of real; The array type can then be specified in the procedure header, for example: Procedure Name_P(p: M); The type of formal parameters is described only in the title of the procedure. A list of formal parameters may be missing, for example. Randomize procedure has no parameters.


If multiple statements result in a single variable value, you can include those statements in the function description. For example, the Sin(x) function; returns the value assigned to the variable Y:=sin(x); (this and other standard functions are described in the System module, which connects to the program automatically).


If several statements result in some action or calculation of several variables, it is better to include these statements in the description of the procedure. For example, the ClrScr procedure; from the CRT module clears the screen.

The procedure is invoked in the execution section of the main program or other procedures (nested procedures). The program (procedure) within which another procedure is called is called external to the procedure being called.


When a procedure is called, the actual parameters whose values are used in the procedure are substituted instead of formal parameters.

For example: Here:
p11, p22, . . . - names or values of variablesp33,
p44, . . . - the names of the variables whose values are returned to the program.
Y is a variable to which the value returned by the function is assigned.


The types of the corresponding formal and actual parameters must match, and the names can be the same or different. Instead of parameter-values, you can substitute variable names, variable values or expressions, instead of variable parameters, variable names are substituted. A function and variable parameters return to an external program the values that are obtained after the function or procedure finishes. Changes in the parameters-values in the procedure are local, in the external program the corresponding actual parameters are not changed. If you do not want to pass new values to an external program, you should use value parameters rather than variable parameters.


You can use local labels, constants, and variables in a procedure by describing them in the procedure description section. Local names cannot be the same as formal parameter names, and their values are not passed to an external program. Labels, constants, and variables that are described in an external program earlier than the procedure itself are called global in relation to the procedure being called. If the local and global names match, the procedure uses local values and the external program uses global values, which means that local and global identifiers are independent. If the name of a global variable is unique (the procedure does not describe a variable with the same name) and its value in the procedure changes, it also changes in the external program. When calling a procedure in a program, the programmer uses its name and parameters without parsing, but often without knowing the contents of the procedure. Therefore, for the sake of universality of procedures, all values should be passed to the procedure through a list of parameters, and variables within the procedure should be described, that is, they should be made local.


Here is an example of the procedure for displaying a programmer's business card on the screen. If the procedure is described in another file with a name, for example, F_PR. pas, then it can be connected to the program by specifying the directive in the description section: {$I F_PR. pas}


Here is an example of using the standard procedures of the DOS module to display the current date and time:


In practical tasks, procedures that return the values of array elements are often written. Here is an example of the procedure for calculating the "N" values of the function, for example, Y = 4 *Sin(x)+7*Cos(x); in the specified range x1<=x<=x2, with N<=100 and uniform range splitting.

Procedure Name_P(p1, p2,...: "type"; Var p3, p4,...: "type";...);
Function Name F("formal parameter list"):"result type";
Name_P(p11, p22,..., p33, p44,...); - call procedure Name_P, Y:= Name_F("list of actual parameters"): - function call Name_F, 
Program NP_1;
Var
   Dat, Fam:string; { Fam: global variable }
ProcedureVIZ(D_R :string); { D_R - formal parameter }
Var
   S_t: string;{ S_t: local variable }
Begin
    Writeln('| ------------------------------------------------------ | ');
    Writeln('| Program Developer:', Fam:14,' |');
    writeln('| |');
  Writeln('| productive quality code ', D_R:14,' |');
  Writeln('| Phone: 0-00-00 |');
    Writeln('| ---------------------------------------------- |');
    Write(' Comment: ');
    ReadIn(S_t)
end;
Begin
   Fam:='I.I. Ivanov';
   Dat:='06.12.95'; {Dat - actual parameter}
   VIZ(Dat); { procedure call }
   readln;
END.
uses DOS; { connect DOS module }
Date Procedure_Time;
var y, m, d, d_w:word; h, min, sec, hund: word;{local options }
begin
   GetDate(y,m,d,d_w); {call DOS procedure returning date parameters }
   GetTime(h,min,sec,hund); { procedure returning time parameters }
   writeln('today: ');
   writeln('_':10, d, ' number');
   writeln('_':10, m, ' month');
   writeln('_':10, y, ' year' );
   writeln('weekday: ', d_w ); { d_w= 0 - Sunday, etc. }
   writeln('Time: ' );
   writeln('_':6, h, ' hours' );
   writeln('_':6, min, ' minutes' );
   writeln('_':6, sec, ' seconds' ); readln
end;
Begin
   Date_Time
end.

 

type r_1000= array[1. . 1000] of real; { type r_1000 is set } var Z: r_1000; x1, x2: real; n:word; Procedure Mas_Y(var Y:r_1000; x1,x2:real; n:word); {Y - parameter-variable} var i: word; x, dx: real; { local parameters } begin If (n>1000) or (n<2) then begin writeln('Array length >1 and must not exceed 1000'); readln; Halt end; i:=0; x:=x1; dx:=(x2-x1)/(n-1); { dx - argument change step } If dx<= 0 then begin writeln('x2 must be greater than x1'); readln; Halt end; While x

 

Here, the type of the formal parameter "Y" is specified in the type description section of the external program and matches the type of the actual parameter "Z", whose element values are returned to the external program.

 

The Halt statement interrupts the execution of the entire program, even if it is used inside a procedure. The use of an Exit statement within a procedure causes an abort in the procedure, but not in the external program. Here is an example of the procedure for outputting an array of numbers to a file:

 

Type M_30х30_r= array[1..30, 1..30] of real; { type M_30x30_r is set }
var x: M_30x30_r;
     i, j, n, m: byte;
{---------------------------------------------------------------- ----------------------}
Procedure Wr_M(a: M_30x30_r; name_f: string; n, m: byte);
Var
  i, j: byte; { a - NxM array, n<=30, m<=30 }
  f:text; { name_f - file name }
begin
  assign(f, name_f);
  rewrite(f);
  For i:= 1 to n do
    begin
      writeln(f);
      For j:= 1 to m do write(f, a[i,j]:6:2)
    end;
  close(f)
end;
{---------------------------------------------------------------- ----------------------}
Begin
  N:= 10; { creating a symmetrical matrix }
  for i:= 1 to N do
    for j:= i to N do
      x[i, j]:= 0.5 + random(50); { filling the upper triangular matrix }
  for i:= 1 to N do
    for j:= i to N do
      x[j,i]:= x[i,j]; { filling in the lower, symmetrical part of the matrix }
end.


  For correct reading of data written to a file by formatless output, a space is required to separate the numbers.


Here is an example of a function for calculating the height of a triangle given the values of its sides.

Program TR;
Var a, b, c, ha, hb, hc: real;
{---------------------------------------------------------------- ----------------------}
Function H_TR(a, b, c: real): real; { a, b, c - Triangle sides }
Var p, s: real;
Begin
  If (a<0) or (b<0) or (c<0) Then
    begin
      Writeln('Triangle sides >0 ?');
      readln;
      Halt
    end;
  If (a>(b+c)) or (b>(a+c)) or (c>(a+b)) Then
    begin
      Writeln('a<(b+c), b<(a+c), c<(a+b) ?');
      readln;
      Halt
    end;
  p:= (a+b+c)/2; { semiperimeter }
  s:= Sqrt(p*(p-a)*(p-b)*(p-c)); { square }
  H_TR:= 2*s/a; { Assigning a value to a function }
end;
{---------------------------------------------------------------- ---------------}
Begin
  Writeln('Enter the sides of the triangle a,b,c');
  readln(a,b,c);
  ha:= H_TR(a, b, c);
  hb:= H_TR(b, a, c);
  hc:= H_TR(c, b, a);
  Writeln('Triangle heights:');
  Writeln('ha=',ha:-10:4, 'hb=',hb:-10:4, 'hc=',hc:-10:4);
  Readln
end.


The program calls the function for calculating the height of a triangle three times for various combinations of actual parameters, which allows you to calculate all the heights of the triangle.

Let's give an example of using the function to calculate the sum of the terms of a power series representing the trigonometric function Y= Sin(x).

PROGRAM fun_sin;
var y, y1, x1: real;
{---------------------------------------------------------------- -----------------------}
Function Sin_r(x: real): real;
Var a, k, y: real; i: longint;
begin
  if abs(x) > 2*Pi Then x:= 2*pi*Frac(x/(2*Pi)); {accounting for periodicity}
  if abs(x) > 2*Pi Then x:= x - 2*pi*Int(x/(2*Pi)); { functions }
  if abs(x) > Pi Then x:= Pi - ABS(x); { taking into account the asymmetry of the function }
  i:= 0; a:=x; y:= a;
  while abs(a)>0.0000001 do
    begin
      i:=i+1;
      k:=-x*x/(2*i*(2*i+1));
      a:= a*k;
      y:= y + a
    end;
  Sin_r:= y; { assigning a value to a function }
end;
{---------------------------------------------------------------- -----------------------}
Begin
  write('Enter the argument value: x1= ');
  readln(x1);
  Y:= Sin_r(x1); { call to a function designed by the programmer }
  Y1:= Sin(x1); { standard function call }
  writeln('argument value: x1= ', x1);
  writeln('calculated value of the function: Sin_r(x1)= ', y :-11:8);
  writeln('test result: Sin(x1) = ', y1:-11:8);
  writeln('Press Enter');
  readln;
end.
graph


  In the description of the function, the operator for assigning the function to its value is required. Changing the value of the "x" value parameter in the function body does not affect its value in the external program. The function returns the value that is assigned to the variable "y" in the external program. </n<=>