home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
MBUG
/
MBUG043.ARC
/
TL14.TXT
< prev
next >
Wrap
Text File
|
1979-12-31
|
7KB
|
253 lines
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 65
TURBO-LESSON 14: INTRODUCTION TO PROCEDURES
OBJECTIVES - In this lesson, you will learn about:
1. PROCEDURE declaration
2. Using a Procedure
3. Using Parameters
4. A counter with error checking
1. PROCEDURE declaration.
The PROCEDURE subprogram is similar to the FUNCTION subprogram
introduced in an earlier lesson. The form of the declaration
is:
PROCEDURE Add(No_1, No_2 : Integer; VAR Sum : Integer);
BEGIN
Sum := No_1 + No_2;
END;
Add is the name of the Procedure.
No_1, No_2, and Sum are integer variables called "parameters".
VAR in front of Sum indicates that this parameter is a "two-way
street". It can receive data from the calling program, and
return data to the calling program. No_1 and No_2 can only
receive data from the calling program.
The BEGIN END block defines the processing performed by the
procedure:
Add the value in the memory location, No_1, to the value in
memory location, No_2, and place the result in the memory
location, Sum.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 66
2. Using a Procedure.
A reference to this procedure in the main program (or another
procedure or function) would have the following form:
Add(2, Count, Adjusted_Count);
The procedure is "called" or utilized by simply using its name as
a statement. (When you define a Procedure or Function, you are
adding additional "statements" to your programming language.)
Notice that there are three "parameters" here, and three in the
Procedure declaration. The three here are associated with the
ones in the declaration by position.
The first parameter here, the integer, 2, provides input to the
first parameter of the procedure, No_1.
The second, the variable, Count, provides input to the second
parameter of the procedure, No_2.
The third, Adjusted_Count, provides input to the third
parameter of the procedure, Sum.
Since Sum is declared VAR, variable, it also provides output back
to Adjusted_Count.
In the Main Program In Procedure Add
2 ----------> No_1 { When Procedure }
Count ----------> No_2 { is called }
Adjusted_Count ----------> Sum { }
---------------------------------------------------------------
Adjusted_Count <---------- Sum { When Procedure ends }
##### DO:
Inspect PROG14.
Run the program.
##### DO:
Add the following statement as the first statement in the main
program:
Sum := 10;
Run the program.
Does it make any difference what value is stored in Sum before
Procedure Add is referenced? Look at the procedure - is Sum used
as an input for the calculation, or only as a result?
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 67
3. Using Parameters.
You have already been using parameters, but in this section, you
will see a little more of the power and flexibility of
parameters.
##### DO:
Change the Add statement in the main program to:
Add(2, 2, Adjusted_Count);
Run the program. Is the result as expected?
##### DO:
Change the Add to:
Add(Count, Count, Adjusted_Count);
Run the program. Any surprizes?
##### DO:
Change the Add to:
Add(2, 3, 4);
Run the program. What happened?
The compiler refused to accept 4 as a variable identifier.
The VAR preceding Sum in the procedure declaration puts a limit
on the corresponding parameter in the calling program: it has to
be a variable location to receive the value of Sum when the
procedure finishes its calculation.
The first two parameters in the procedure, No_1 and No_2, are
used only for input to the procedure - they do not provide any
output back to the corresponding parameters in the calling
program.
For this reason, the first two parameters in the calling program
are less restricted. They can be constants, variables, or
expressions, as long as they are of the same type, integer, as
the corresponding parameters in the procedure.
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 68
##### DO:
Change the Add statement:
Add(4, 2 * Count + 5, Adjusted_Count);
Run the program. Does the expression work o.k. as a parameter?
##### DO:
Change the Add to:
Add(Count, Count, Count);
Also change the WriteLn to print the value of Count instead of
Adjusted_Count.
Run the program. Any problems?
4. A counter with error checking.
You could use the Procedure Add as a counter by using the
following call:
Add(Count, 1, Count);
The procedure would add 1 to Count and put the result in Count.
You could accomplish the same thing with the statement
Count := Count + 1;
So why bother to use the procedure?
What if Count reaches the upper limit of the integer range,
32767?
In the main program you could expand the counting statement to:
IF Count < 32767
THEN
Count := Count + 1
ELSE
WriteLn('Counter reached upper limit');
î
TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 69
PROGRAMMING NOTE: HOW DO YOU DECIDE WHAT GOES INTO THE MAIN
PROGRAM AND WHAT TO PUT IN SUBPROGRAMS?
FROM THE FOLLOWING DISCUSSION, TRY TO FORMULATE
AT LEAST ONE RULE FOR MAKING THIS DECISION.
This looks a little messy, maybe you should use a procedure to
get this out of the main program.
Not a bad reason, but there's a more important reason:
Ask yourself, "How much of the processing in the IF statement
above is of interest in the main program?"
Probably only the fact that a count is being incremented. The
error checking and how it is done is probably of little interest
and just clutters up the main program.
##### DO:
Write your own procedure to increment the counter, Count.
Call the procedure, Increment.
Check for the upper limit of the integer range. (The IF
statement above would be one way.)
Note that only one parameter is needed, preceded by VAR.
##### DO:
In the main program, add the following to check out your
procedure:
FOR I := 1 to 10 DO
BEGIN
Increment(Count);
WriteLn('Count = ', Count);
END;
Run the program using the following values as input for count:
0, 3, -34, 32760
(PROG14A is provided in case you need help.)
You could also write a procedure, Decrement, to decrease a
counter. Note that the error checking would be checking the
lower limit, -32768, or perhaps 0 depending on how you intended
to use the counter.
î