Unit 6.2 - Procedures and Parameters

PROG

read in the dimension
compute the area of the square side = dimension
compute the area of the circle radius=dimension
compute the area of the square side = 2*dimension
compute the area of the circle radius=2*dimension
(same as Unit 6.1)

SECOND PROGRAM

increment(x,y) -- adds x to y

1. reads in a and b
2. adds b to a
3. reads in c and d
3. add c to d

PED

to learn procedures, with parameters,

CONCEPTS

procedure proc-name(identifier1:type;identifier2:type;identifier3:type...);

To call

proc-name(argument1 , argument2 , argument 3 ....

Upon call

identifier1 < - argument1
identifier2 < - argument2
identifier3 < - argument3


var parameters

proc proc-name(identifier1:type;identifier2:type;identifier3:type...;
var identifierA:type;var identifierB:type;var identifierC:type...);

To call

proc-name(argument1 , argument2 , argument 3 ....;
argumentA ,argumentB ,argumentC);

Upon call

identifier1 < - argument1
identifier2 < - argument2
identifier3 < - argument3
identifierA <- argumentA
identifierB <- argumentB
identifierC <- argumentC

Upon return

argumentA <- identifierA
argumentB <- identifierB
argumentC <- identifierC


Unit 6.2 -- Procedures with Parameters

In our previous unit, we saw how much of a pain it could be when we don't have parameters. If we want the same procedure to work on different variables, then we need parameters.

Parameters refer to the names of the results inside the subroutine. Arguments refer to the variable sor expressions that appear in the call to the procedure.

In
procedure proc-name(identifier1:type;identifier2:type;identifier3:type...);

identifier1 is the first parameter.
identifier2 is the second parameter
identifier3 is the third parameter

Each parameter must have a type: integer, real, string, etc.

To call we write:

proc-name(argument1 , argument2 , argument 3 ....);

Upon call to the subroutine, the follwoing implicit assignments occur.

identifier1 < - argument1
identifier2 < - argument2
identifier3 < - argument3

Please note that:

1.    The type of each argument must match that declared for the corresponding identifier in the
      procedure definition.  If we declare
procedure xyz(a:integer,b:real);
begin

end;

      and the main program had
      var z:integer; 
u:real;
      we could write 
xyz(z,u)
      but not
      xyz(u,z)

      In the latter case we would have a type mismatch as a is integer and the corresponding
      parameter, u, is real.  In addition, the type of z as an argument doesn't match the type of b as
      a parameter.

2.    Any changes to a and b inside xyz will not affect z and u in the main program.  We would need
      var parameters for such changes to occur.
In a var parameter, not only do we copy the contents of the argument to the parameter when we call the procedure, we also copy the contents of the argument back to the parameter when we return from the procedure. This allows us to write subroutines that change our argument.

More generally,

proc proc-name(identifier1:type;identifier2:type;identifier3:type...;
var identifierA:type;var identifierB:type;var identifierC:type...);

To call

proc-name(argument1 , argument2 , argument 3 ....;
argumentA ,argumentB ,argumentC);

Upon call

identifier1 < - argument1
identifier2 < - argument2
identifier3 < - argument3
identifierA <- argumentA
identifierB <- argumentB
identifierC <- argumentC

Upon return

argumentA <- identifierA
argumentB <- identifierB
argumentC <- identifierC

Note that var parameters are copied from main program to subroutine when you call the subroutine and are copied back when you finish. Parameters that are not var parameters are only copied one way. This prevents inadvertant changes to variables in the main program.

Let's look at our two examples. The first example again computes the Area of a square whose side is dimension, then a circle whose radius is dimension. Then both computations ar erepeated with twice the value of dimension.

The procedure AreaSquare is defined on lines 3 to 6. On line 6, note the single parameter "side:real" Area is computed in line 5 and left in the variable "Area"

AreaCircle is similar. It can be found in lines 7 to 10. It's parameter is called "r" Again, it leaves the result in Area.

Now let's look at the calls.

Line 14. We compute the area of the square whose side=dimension. Dimension is the argument and matches the single argument side in line 3.

Line 17. We compute the area of the circle radius=dimension. Dimension is the argument and matches the single argument r in line 7.

Line 21. We compute the area of the square whose side=2*dimension. Note that the expression, 2*dimension is assigned to side, the corresponding argument.

Line 24. We compute the area of the circle whose radius=2*dimension. This expresion is computed and assigned to the variable r.

Question for thought: How could we use "var" parameters to avoid having to get the answer out of the global variable, "Area"

The second example illustrates "var" parameters. A procedure called increment will add the second argument to the first argument. The first argument is changed. Thus, it must be a "var" parameter. The first parameter is called x and the second one y.

Note that line 6 attempts to increment y. Note that this won't affect the second argument when we call it--no affect.

The first call is in line 12, This adds b to a. (That is, b is copied to y, a is copied to x. Then x gets changed to the sum of the two parameters. Y inside the procedure gets changed. However, when bc corresponds to x and d corresponds to y. Again, at the end, c will be changed to the sum of c and d and d will remain unchanged.



Assignment Set Eight

For sneak preview students, the following will count as an A work if done by September 24th of this year. It must be done on the computer.

Those completing the assignment after this date will have to do additional problems to earn even a C. And those wishing to earn a B or an A, will have a different problem to do. The B problem will involve passing of arrays to a subroutine or function. This will be the normal way that Assignment Sets are prepared, so this situation where a single problem earns an A is a special reward for those who do things early!

So the moral is, do this problem early if at all possible!!!!

Write a procedure called sumprod.

It takes four arguments. The first two are input and the second two are output.

They are:

1.    An input variable, input1

2.    An input variable, input2

3.    An output variable, sum

4.    Another output variable, product. 
The sumprod procedure should put the sum of the first two arguments, input1 and input2 into sum and the product of the second two arguments into the variable product.

You should have four calls to the procedure Sumprod. Of course, these all appear in your main program.

In the first call,
C should be set to the sum of A and B and D should be set the product of A and B

In the second call,
G should be set to the sum of E and F and H should be set to the product of E and F.

In the third call,

Grand_sum should be set to the sum of C and

in the fourth and final call,
Grand_Prod should be set to the product of D and H.

Your main program should read from the keyboard, A,B, E and F and print out C, D, G, H, Grand_sum and Grand_Prod.

Thanks for getting this in early!