Chapter 7
Functions and Procedures
So far, all the programs in this tutorial have been contained inside
a single block. This has been fine since all of the programs in this tutorial,
so far, have been very short. Larger programs should be divided into smaller
blocks called SUBPROGRAMS. Subprograms are implemented in Pascal as FUNCTIONS
and PROCEDURES.
PROCEDURES
Program Intro_To_Procedures (input, output);
var
i : integer;
{The following is a procedure}
{it looks like a small program}
Procedure Sample_Procedure;
Begin
Writeln('Greetings from');
Writeln('the Sample_Procedure');
Writeln('now back to your );
Writeln('regularly scheduled program');
End;
{now begins the main block}
{ of the program.}
Begin
Sample_Procedure; {call the subprogram}
Writeln('The End');
End.
Procedures go between the variable section and the main program. A procedure
must have a procedure heading. The procedure heading is similar to the program
heading. It starts with the keyword "PROCEDURE." It must be followed
by the name of the procedure. The name must be a valid identifier. In the
example above, the procedure name is followed by a semicolon. This means
that no variables must be passed to the procedure.
Some procedures require that variables be passed to them. These variables
are called arguments. Take the following program for example. It includes
a procedure that multiplies the first argument by ten and the second argument
by twenty.
Program Proc_with_args(input, output);
var
i : integer;
Procedure Multiply(x : integer; y : integer);
var
z : integer
Begin
z := 10 * x; {multiply the argument by 10}
Writeln('10 multiplied by ', x, ' is ', z);
z := 20 * y;
Writeln('20 multiplied by ', y, ' is ', z);
end;
Begin
For i := 1 to 10 do Begin
Multiply(i, i); {call the procedure}
End;
End.
In this example the procedure heading is slightly modified. Instead of
being followed by a semicolon, the procedure name is followed by an argument
list. The argument list tells the compiler how many arguments the procedure
takes and their datatypes.
GLOBAL AND LOCAL VARIABLES
Notice, in the example above that there is a VAR section after the procedure
heading. This VAR section allows LOCAL VARIABLES to be declared. Local Variables
are variables that exist only inside a function or procedure. They can only
be accessed from within the function or procedure. In previous examples,
all programs had GLOBAL VARIABLES. Global variables are declared in the
VAR section below the program heading and can be accessed by the main program,
all procedures, and functions. Local variables should be used whenever possible.
Since they can only be accessed from within the function or procedure that
they are in, there is less of a risk that they will be accidentaly modified.
FUNCTIONS
Functions are similar to procedures. Functions in Pascal are also similar
to functions in algebra. They take a value, perform various calculations,
and then return a new value. Take the following for instance.
Program Intro_to_functions (input, output);
Var
i : integer;
n : integer;
Function Test_Function( x : integer):integer;
Begin
{This function performs the }
{ mathematical equivalent of}
{ f(x) = 2x + 1 }
Writeln('performing calculation');
Test_Function := 2 * x + 1;
End;
Begin
for i := 1 to 10 do begin
n := test_function(i);
Writeln(n);
end;
End.
In this example the argument list is followed by a colon and a datatype.
This tells the compiler what type of data the function returns. In this
case the function returns an integer. To return a value the return value
is assigned to the name of the function.
 |