CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Procedures

1. Divide and Conquer problem solving
One powerful technique for solving big problems is to divide the big problem into smaller problems, each of which is easier to solve.

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:

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:

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:

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:

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


In memory, there will be two different locations, one for number and one for num:

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:


Here, the variable, number, and the parameter, num, both refer to the same location in memory. So when the procedure doubles the value of num it also doubles the value of number.