CSCI 150, Spring 2003
Home | | Course Schedule | | Assignments | | Lecture Notes
We can capture this method of problem solving using the Pascal subprograms called Procedures and Functions.
2. Writing Procedures:
A procedure is defined by using the keyword, procedure, followed by the name of the procedure,
followed by the program code to be executed when the procedure is called.
Example:
procedure ShowCredits; begin writeln('-------------------------------------------'); writeln('The Calculator Program'); writeln('By M.C. Squared'); writeln('-------------------------------------------'); end.
3. Calling a procedure.
To call a procedure that has no parameters, just use the name of the procedure as a program
statement. Note that the definition of the procedure must be included in the program.
Example:
program myCalculator; procedure ShowCredits; begin writeln('-------------------------------------------'); writeln('The Calculator Program'); writeln('By M.C. Squared'); writeln('-------------------------------------------'); end; var {Declarations for calculator program go here} begin ShowCredits; {Code for doing calculations goes here} . . . ShowCredits; end.
4. Execution of a procedure.
When the program execution reaches a procedure call, the program control is transferred to the
procedure and the code in the procedure definition is executed. When control reaches the end of
the procedure, it then returns to the calling program and continues execution of that program.
Output of above example:
------------------------------------------- The Calculator Program This is from the first call to ShowCredits By M.C. Squared ------------------------------------------- ---Calculator operations ------- ------------------------------------------- The Calculator Program This is from the second call to ShowCredits By M.C. Squared -------------------------------------------
5. Using parameters.
Parameters allow the calling program to pass information to the procedure. This can make
procedures much more general in their use.
Example:
program CSLike; procedure DisplayMessage(n: integer); begin writeln('I have liked CS for ', n, 'years'); end; begin DisplayMessage(15); writeln('Goodbye'); end.
In the above example, n is called the formal parameter. The number 15 is the actual parameter. In general the formal parameters are those listed in the procedure header. The actual parameters are those given when the procedure is called.
Example 2:
program PollutionMessage; procedure WriteMessage(Index: integer); begin if (Index < 35 ) then begin writeln('Pleasant'); end else if (Index <= 60 ) then begin writeln('Unpleasant'); end else begin writeln('Health Hazard'); end; {end if then else } end; {end WriteMessage} var PollutionIndex: integer; begin write('Enter air pollution index: '); readln(PollutionIndex); WriteMessage(PollutionIndex); end.
The actual parameter here is PollutionIndex. The formal parameter is Index. When the program is executed, a copy of the value that is stored in PollutionIndex is passed to the memory location labelled by Index. E.g. suppose the user enters 50 when prompted. This value will get stored in PollutionIndex when the readln is executed. The value 50 will then be passed to Index when the WriteMessage statement is executed.
Output:
6. Pass-by-value and Pass-by-reference. In the above example, only the value of the actual parameter is passed to the procedure. This is called pass by value, and it is the default method of passing values to Pascal procedures. Because a copy of the parameter is made, the procedure cannot change the value of the actual parameter.
Example:L
program doubleTrouble; procedure double(num: integer); begin num := num * 2; writeln('The double is ', num); end; var number: integer; begin number := 3; double(number); writeln('The number is ', number); end;
when the procedure doubles the value in num, it doesn't affect the value stored in number.
Output:
The double is 6 The number is 3
Pass-by-reference:
Sometimes we would like the procedure to modify the value of the actual parameter. In this case
we use pass-by-reference. In pass-by-reference, the address of the actual parameter is passed to
the procedure. Thus, when the procedure changes the value of the formal parameter, the value of
the actual parameter changes too (because they both refer to the same memory location). To use
pass-by-reference you must include the keyword var in the formal parameter list of the procedure.
Example:
program doubleDouble; procedure double2(var num: integer); begin num := num * 2; writeln('The double is ', num); end; var number: integer; begin number := 3; double2(number); writeln('The number is ', number); end;