home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
PASCAL
/
PASTUT34
/
PROCFUNC.TXT
< prev
next >
Wrap
Text File
|
1993-06-12
|
4KB
|
87 lines
PROCEDURES AND FUNCTIONS.
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Procedures and functions allow additional blocks to be nested in the main
program block. Each procedure or function declaration has a heading
followed by a block. A procedure is activated by a procedure statement; a
function is activated by the evaluation of an expression that contains its
call and returns a value to that expression.
A procedure heading consists of the reserved word PROCEDURE, followed by
the procedure identifier (or name) and followed optionally by any formal
parameters. The procedure body consists principally of a block of Pascal
statements, but also can include assembler code and reserved words like
NEAR and FAR (to be discussed later).
A function heading consists of the reserved word FUNCTION, followed by the
function identifier, an optional parameter list, a colon and then the
result type. The function body consists principally of a block of Pascal
statements, but also possibly assembler code and other reserved words.
For both procedures and functions, each parameter declared in a formal
parameter list is local to the procedure or function being declared and can
be referred to by its identifier in the block associated with the procedure
or function.
The formal parameter list is enclosed in parentheses and concluded by a
semi-colon. There are three kinds of parameter: value, variable and
untyped variable.
A parameter group without a preceding VAR and followed by a type is a list
of value parameters (when the value of the parameter is changed it only
affects a temporary store and not the actual variable outside the
procedure).
A parameter group preceded by VAR and followed by a type is a list of
variable parameters. Such parameters are called reference parameters and
changes made to the variable within the procedure remain even after the
procedure has ended.
A parameter group preceded by VAR and not followed by a type is a list of
untyped variable parameters.
See Chapter 8 of the Programmer's Guide (pp 95- 112) for further details.
A simple example of a procedure is illustrated in the program PROC.PAS
which has a procedure TimesTable, which lists on the screen any chosen
part of a multiplication table, for multiplicands and multipliers all less
than 100. This procedure is listed below:
procedure TimesTable(BaseValue,Lower,Upper: integer);
begin
writeln;
writeln(BaseValue,' Times Table');
writeln;
For i:=Lower to Upper do writeln(BaseValue:2,' * ',i:2,' = ',BaseValue*i:5);
end;
In the main part of the program, the user is asked for values of the three
parameters and then effectively the procedure is called as follows:
TimesTable(57,10,20);
which then displays 57 * 10 = 570
57 * 11 = 627
etc. to 57 * 20 = 1140
An example of a function is illustrated in the program FUNC.PAS which
defines a function as follows:
function Power(Base, Exponent : real) : real;
begin
.....
Power := exp(Exponent * ln(Base))
.....
end;
This enables the evaluation of any number to any power, provided the
result does not involve complex numbers. Thus if number is negative
and power is fractional, a warning is given.
The function is effectively called as for example: Result := Power(2,3);
although the example program involves a user interaction for the values.
PROCFUNC.TXT
14.1.93