Unit 6.4 - Passing arrays to functions and procedures

PROG

Vector -- New Type (fancy word for a one-dimensional array)
ZeroVector -- Zeros out a Vector
InitVector -- element 1 gets 1, second element gets 2, ith element gets i
SumVector -- function, computes Sum of passed Vector

program

Zeros X
Prints out sum of elements
Inits Y
Prints out sum of elements

PED
1. to learn how to pass arrays to functions and procedures
2. to learn how to declare arrays with subscripts with const
3. to learn how to create "types"

CONCEPTS

Order of things in program

program
const
type
var
procedures and functions

begin
main program
end;

const

A constant declaration is of the form

const identifier=value

Can be used anywhere a number can be used.

Including subscript ranges

variables not allowed in subscript ranges

Advantage: allows us to change range easily

type

Format:

type
type-identifier=some kind of type

type-identifier can be used anywhere integer, string, real, etc. can be used

passing arrays

When we pass items to procedures, both things must be the same type

type type-name=array[range] of type;

Use type-name in procedure and to declare item being passed.


Unit 6.5 -- Passing Arrays to functions and procedures

We have learned how to pass various kinds of integers, strings, real numbers to functions. Unfortunately, PASCAL doesn't let us pass arrays (one or two-dimensional) as simply.

Why? When we pass an argument to a PASCAL procedure, both items must be the same type. For example, we could write

program main;
var z:integer;
procedure blah(x:integer);

and then later on, we could write:

blah(z);

We can pass z as the argument corresponding to the parameter x because both x and z are integer.

If we wrote
program main;
var z:real;
procedure blah(x:integer);

and then in the main program, we wrote:

blah(z);

we would get an error message from the compiler. Why? Because z is a real number and x is an integer. They are not the same type.

One would assume if one wrote:

program main;
var z:array[1..10] of integer;
procedure blah(x:array[1..10] of integer);

and then in the main program

blah(z);

that this would work. After all, they are both array[1..10] of integer. But what is obvious is not true. One would get a type error.

Both of the same items are considered by the compiler of two different types. This is even though both types actually correspond to the same thing: array[1..10] of integer;

The way around this problem is to create a new type. This new type is "array[1..10] of integer;"

In the above example, one would write:

program main;
type Vector=array[1..10] ;
var z:Vector;
procedure blah(x:Vector);

In this case, we have a user-defined type called Vector. Now both x and x are of the same type. We can call procedure blah with z as an argument.

I use the word "Vector" because Vector is a mathematical term for a list of integers. You will see the same term in linear algebra, if you take this course. You could, of course, call your array of integers a type of Sugar or a type of Blah.

One more thing that is very useful to do is to define constants. This is done with the "const" command. We simply put the keyword "const" write after the program statement that goes on the first line. After this we write the form identifier-name=value; for one or more identifiers. We ccan use this name anywhere we can put an integer. This includes in expressions in the program itself. It also includes the range-part of the array declarations. That is we have

const n=10;

we could write:
array[1..n] of integer

instead of the usual

array[1..10] of integer;

We can also write code to go through the entire array:

for i:=1 to N do begin
....
end;

By doing this, should we decide to change the size of the array from 10 to 5, we could simply change the const line to n=5. We wouldn't have to change the 10 in two places, the array declaration and the for loop.

Let's look at our sample program.

This procedure defines a type called Vector on line 3. This is a one dimensional array whose subscripts are 1 to VSize. Note the const definiton in line 2. Since VSize is 3, all the items declared vector are arrays going from 1 to 3.

X and Y are both variables of type vector.

ZeroVector (lines 5 to 11) and InitVector (lines 12 to 18) both initialize a Vector. ZeroVector sets every single element to zero. InitVector initializes each element of the Vector to the value of the subscript. If VSize were three, then V[1] would be 1, V[2] would be 2 and V[3] would be three. Note that in both of these procedures, we use VSize to tell when to end the for loop. Note the use of VSize in lines 8 to 15. If we change the size of the Vector by changing the constant VSize, then the initialization code would also initialize each of the elements.

The function SumVector will return the sum of the indicated vector.

Our main program will Zero out X with ZeroVector in line 29. Then it will print out the sum of this vector in line 30. Of course, the sum printed will be zero.

In line 32, we initialize Y with InitVector. Thus Y[1] would be 1; Y[2] would be 2; Y[3] would be 3. In line 33, we take the sum of this. The value of the sum would be 6.

Some additional comments on typing follow.

Because of the limitation on the matching of types of arrays, it is impossible in PASCAL to write a general routine for arrays, e.g., printing out a two-dimensional array or summing a vector, no matter what the subscript size is. Often, one would have in a program several different arrays, each with different dimensions. It might be desired to have a routine that will print them out or zero them or whatever. One would have to make a type for each different dimension size and write a different subroutine for each one. This is not a problem in PL/1, ADA or LISP. It is impossible on PASCAL

Remember that the order of things in a PASCAL program is

program statement
const
type
var
procedure and function declarations

begin
main program
end;