Chapter 1
Introduction to DOS
Chapter 2
Introduction to Turbo Pascal
Chapter 3
Parts of a Pascal Program
Chapter 4
Control Structures and Looping
Chapter 5
Looping
Chapter 6
Procedures
Chapter 7
Parameters Passing
Chapter 8
Functions
Chapter 9
Arrays
Chapter 10
Searching and Sorting
Chapter 11
Records and File of Records
|
CHAPTER 6
( Procedures )
We have
already seen how loops can save a lot of programming time and
computer memory by drastically reducing lines of code. What if
you want a program do two different things between each
iteration? Suppose you want to count 1 to 100, then do ABCs,
then 1 to 100 again, and so on. None of the loop structures we
learned will do this without writing the loops over and over
again. Procedures and functions are just the answer to this
dilemma. In this chapter we will explore procedures; chapter 7
will deal with functions.
|
PROGRAM 6-1
Program Stanza (output);
Procedure Chorus;
begin {chorus}
end;
begin {stanza}
writeln('Mankind takes defeat, the
nations are suppressed');
writeln('The confusion evolves to fear and the thinkers are
distressed');
chorus;
writeln('They search for alternatives and another temporary solution');
writeln('Imagination turns to reality; this is no illusion);
chorus;
writeln('Salvation is met in a form that is different and new');
writeln('The inanimate is animated, and is named CPU');
chorus;
writeln('Its existence has provided a more vast insight');
writeln('What's hidden is discovered and the dark turns to light');
chorus;
end.
|
Program run:
Mankind takes defeat, the nations are
suppressed
The confusion evolves to fear and the thinkers are
distressed
CPU, he helps us through and through
He makes jobs today seem like nothing but child's
play
They search for alternatives and another temporary
solution
Imagination turns to reality; this is no illusion
CPU, he helps us through and through
He makes jobs today seem like nothing but child's
play
Salvation is met in a form that is different and
new
The inanimate is animated, and is named CPU
CPU, he helps us through and through
He makes jobs today seem like nothing but child's
play
Its existence has provided a more vast insight
What's hidden is discovered and the dark turns to
light
CPU, he helps us through and through
He makes jobs today seem
like nothing but child's play
Type EXIT to return to Turbo Pascal...
Microsoft(R) MS-DOS(R) Version 5.00
(C)Copyright Microsoft Corp 1981-1991.
Suppose you wish to write a song with stanzas and
chorus. You can write it two ways. At the end of each stanza write
the chorus. This means you will have to repeat the chorus several
times. Or you can write the chorus at the bottom after all the
stanzas are written. You just need to indicate when to sing the
chorus. I told my children my need to illustrate this concept. They
came up with a song which is used in Program 6-1. I don't know if it
meets all the criteria for a song, but it certainly gets my point
across.
I called the program Stanza and the procedure
Chorus. The chorus is declared before it is called from the main
program. Every time the chorus needs to be written, we just call the
chorus. This program writes two lines of chorus after each two lines
of stanza and then leaves a blank line. I may not have impressed you
very much with this program since the procedure is only called four
times; you could have just as easily typed it four times instead.
But consider that a program may call a procedure thousands of times
thereby saving tens of thousands of lines of codes.
A procedure is a subprogram within a program. As
you will see a procedure has all the components of a program. In
Pascal all subprograms must be declared before the beginning of the
main program. After its declaration, a procedure may be called many
times from within a program; it is called by its name. When the call
is made the subprogram becomes active and completes its assigned
task. Then the execution resumes with the next statement in calling
program.
MORE ABOUT PROCEDURES
Procedures allow us to modularize programs. A good
rule of thumb should be that a module (procedure) should not be any
longer than a screen full (20 to 25 lines). This allows for easy
reading and debugging. Program 6-2 converts temperature from one
unit to the other. The program is modularized in that the Fahrenheit
to Celsius conversion is done by one procedure and the Celsius to
Fahrenheit conversion is done by another procedure.
This program will ask you whether you want to
convert Fahrenheit or Celsius. It will then ask for the temperature.
This temperature is then passed to the appropriate procedure to be
converted. The result is printed from within the procedure.
Whatever you pass to a procedure is known as
arguments. Arguments or parameters are enclosed in parentheses. In
this example the temperature is the argument. You could pass, none,
one or more arguments to a procedure. Program 6-1 did not pass any
arguments to the procedure. In this temperature conversion program
one argument is passed to the procedure.
Let me illustrate how a procedure might be applied
to everyday life. In this example the procedure is behaving like a
toll collector at the tollgate. You pass a certain amount of money
to the toll collector, and he opens the gate for you. In this
example the money would be the argument. An argument is also known
as a parameter.
PROGRAM 6-2
Program Convert (input,
output);
var
temperature : real;
whichOne : char;
(*******************************************************)
Procedure ConvertToCelsius(Fahrenheit:real);
begin
Celsius := (Fahrenheit-32)*5/9;
write(Fahrenheit:5:1 ,' degrees fahrenheit is ');
writeln('equal to ', Celsius:5:1,' degrees Celsius.');
end;
(******************************************************)
Procedure ConvertToFahren(Celsius:real);
begin
Fahrenheit := Celsius*9/5+32;
write(Celsius:5:1,' degrees celsius equals ');
writeln(Fahrenheit:5:1,' degrees fahrenheit.');
end;
(******************************************************)
begin {main Program}
write('This program will convert
Fahrenheit');
writeln(' to Celsius or Celsius to Fahrenheit ');
writeln;
write('Convert Celsius or Fahrenheit (C/F) ');
readln (whichOne);
writeln;
write('Enter temperature ');readln(temperature);
Case whichOne of
'C' :
ConvertToFahren(temperature);
'F' :ConvertToCelsius(temperature);
else writeln('You did not enter C or F as instructed!');
end;
end.
|
Program run:
This program will convert Fahrenheit to
Celsius or Celsius to Fahrenheit.
Convert Celsius or Fahrenheit (C/F)? F
Enter temperature 100
100.0 degrees fahrenheit is equal to 37.8 degrees
Celsius.
This program will convert Fahrenheit to Celsius or
Celsius to Fahrenheit.
Convert Celsius or Fahrenheit (C/F)? C
Enter temperature 100
100.0 degrees celsius equals
212.0 degrees fahrenheit.
EXPLANATION OF THE PROGRAM
We will be using only two variables in the main
program, temperature and whichOne. The variable "temperature" will
be used to store your temperature input. Variable "whichOne" will be
used to store a character of your choice, F or C. We will be using
two procedures as well, ConvertToCelsius and ConvertToFahren.
Let us now look at the main program block. The
first few write and writeln statements are used to print an
introduction to the program on the screen, and to suggest to enter
either C or F for the next input. The first readln statement accepts
a character input from the keyboard. This character input is stored
in the variable called "whichOne". The next readln statement accepts
an integer entered at the keyboard, and this input is stored in the
variable called "temperature".
Once the input is completed, the program makes
certain decisions using the CASE statement. If variable "whichOne"
had a value of 'C', then the program calls ConvertToFahren
procedure. If it had a value of 'F', then the program calls
ConvertToCelsius. If the input it received was not either 'C' or 'F'
then the program will print out an error message, "You did not enter
C or F as instructed!".
There are two procedure calls in this program. They
are:
ConvertToFahren(temperature);
ConvertToCelsius(temperature); |
As you can see a procedure call is very simple;
just call the procedure by its name. If you have anything to pass to
the procedure, enclose it in the parentheses. What is in the
parentheses in a procedure call is known as the actual parameter
list. How does Pascal know that these are procedures? You declared
them as procedures.
Next, let us look at a procedure closely. A
procedure has a heading like this one:
Procedure ConvertToCelsius
(Fahrenheit:real); |
'Procedure' is a reserved word in Pascal.
'ConvertToCelsius' is the name (identifier) I chose to call this
procedure. 'Fahrenheit' is the variable it will use to receive
arguments from the procedure call. This variable is declared to be
real. You need to pay special attention to this. The procedure call
passes the actual parameter 'temperature' and the procedure receives
it into 'Fahrenheit'. Please note that 'temperature' also was
declared to be real.
The procedure can have its own local variables.
These variables will be used exclusively by this procedure, not by
the main program. There is one local variable declared, celsius. Now
we have everything needed to do the conversion. Celsius is derived
from Fahrenheit which was passed to the procedure. The procedure
displays the results on the screen. Here is a block diagram that
shows the relationship between the main program and the procedures.
Look at the above diagram. The program convert has
access to temperature, whichOne, ConvertToCelcius and
ConvertToFahren. Whatever is placed in temperature will be copied
into Fahrenheit if ConvertToCelsius is called or into celsius if
ConvertToFahren is called. The call will depend upon the value of
'whichOne'.
APPENDIX 6A
Let us look at the process of writing a procedure.
A procedure is constructed just like a program. Instead of the
reserved word PROGRAM, we use the reserved word PROCEDURE. A name is
given to the procedure (just as the program name), then the
parameters passed back and forth are listed in parentheses. Recall
that in a program inputs and outputs are listed in the parentheses
as well. A procedure has all the other components of a program as
well: type, constants, variables, and the program block.
In Pascal, a procedure should be defined first
before it can be called. (There is a way around it, but that is for
later). Therefore, the main part of the program is written at the
bottom of the total program. Calling a procedure is easy, just use
the name of the procedure and then in parenthesis list all
parameters passed to that procedure.
Parameters listed in the procedure call are the
actual parameters. The parameters listed in the procedure heading
are referred to as the formal parameters. These names (identifiers)
do not have to be same as the actual parameters. They just need to
match as far as the number of parameters and the type. The first
parameter in the actual parameter list will pass its value to the
first parameter in the formal parameter list. The second parameter
in the actual parameter list will pass its value to the second
parameter in the formal parameter list, and so on. In other words,
values are passed by their position rather than their names. When
passing variables we must make sure that the receiving variable is
the same type as the value it is receiving. For example, you cannot
pass a real value to an integer variable. You cannot pass a
character to a number nor a number to a character.
At this point it is essential
that you comprehend the concept of global variables and local
variables. When an identifier is used within a procedure, Pascal
first looks to see if it is declared within that procedure either as
a local variable or as a formal parameter. If it is declared then it
stops searching. If it does not find the variable locally then it
looks to calling module. If that identifier is found in the calling
module, or in the module where the calling module is nested, then it
is called a global variable. Here are some block diagrams to
illustrate my point.
ProcedureThree
is global to no other procedures.
ProcedureOne
is global to ProcedureThree.
ProcedureTwo
is global to no other procedures.
Main_Program
is global to ProcedureThree,
ProcedureOne and
ProcedureTwo.
Main_Program can access:
A, B, C which are declared in the Main_Program.
ProcedureOne and ProcedureTwo.
Main_Program cannot access:
Any of the variables declared locally in the
procedures,
i.e. A, B, X, Y, R.
ProcedureOne can access:
A, B, X which are declared in ProcedureOne.
ProcedureThree.
C as global which is declared in the Main_Program.
ProcedureOne cannot access:
Any of the variables that are declared locally
in
ProcedureTwo or in ProcedureThree.
i.e. A, Z, X, Y, R.
ProcedureTwo can access:
A, X, Y, R which are declared locally in
ProcedureTwo.
B, C as global which are declared in the
Main_Program.
ProcedureTwo cannot access:
Any of the variables that are declared locally
in
ProcedureOne or in ProcedureThree.
i.e. A, B, X, Z
ProcedureThree can access:
A, Z which are declared locally in
ProcedureThree.
B, X as global which are declared in
ProcedureOne.
C as global which is declared in the
Main_Program.
ProcedureThree cannot access:
Any of the variables that are declared in
ProcedureTwo.
Any of the variables in ProcedureOne or in
Main_Program
if the same variable name is used locally in
ProcedureThree.
Well, is there an easier way to figure out what can
be accessed? Yes. Take for example ProcedureThree. Obviously it can
access all the local variables declared within. Because
ProcedureThree is nested within ProcedureOne, it can also access any
variable declared in ProcedureOne as long the same variable name was
not already used as a local variable. Furthermore, since
ProcedureOne is nested within the Main_Program, it can also access
any variable declared in the Main_Program as long as the same
variable name was not already used as a local variable in
ProcedureThree or in ProcedureOne. In summary, a procedure can
access all its local variables and any global variable if the same
identifier was not used within the local declaration.
ASSIGNMENTS FOR CHAPTER 6
1. Refer to Program 6-3. What would be the output
of this program.
2. Program 6-3 has several comments with blank
spaces. In these blank spaces indicate local
or global.
PROGRAM 6-3
Program GlobalLocal (output);
{This program illustrates the concept of
local and global variables}
var
(***********************************************)
Procedure ProcedureOne;
Var
(*---------------------------------------------*)
Procedure ProcedureThree;
Var
begin {ProcedureThree}
A := 100; {A is ____________}
Z := A+B+C; {Z and A are _______, B and C are _________}
write ('ProcdureThree, Values of A, B, C, and Z: ');
writeln(A:5, B:5, C:5, Z:5);
end; {ProcedureThree}
(*--------------------------------------------*)
|
Program continued..
begin {ProcedureOne}
A := 40; {__________}
B := 50; {__________}
X := C+A; {X and A are ________, C is __________}
write ('ProcedureOne, Values of A,B,C and X:');
writeln (A:5, B:5, C:5, X:5);
ProcedureThree; {calling
ProcedureThree}
end; {ProcedureOne}
(**********************************************)
Procedure ProcedureTwo;
Var
begin {ProcedureTwo}
A := 100; {_________}
X := A + B; {X and A are local, B is global}
Y := 110; {_________}
R := Y+C; {R and Y are local, C is global}
write('ProcedureTwo, Values of A,X,Y, and R: ');
writeln(A:5, X:5, Y:5, R:5);
end; {ProcedureTwo}
begin {main program}
A := 10;
B := 20;
C := 30;
write ('Main program, Values of A,B,C : ');
writeln(A:5, B:5, C:5);
procedureOne; {procedure calls}
ProcedureTwo;
end. {main}
|
Program run:
Main program, Values of A,B,C : 10 20 30
ProcedureOne, Values of A,B,C and X: 40 50 30 70
ProcdureThree, Values of A, B, C, and Z: 100 50 30
180
ProcedureTwo, Values of
A,X,Y, and R: 100 120 110 140
Type EXIT to return to Turbo Pascal...
Go to top of this chapter
|