Procedure Parameters and Local Variables

Variables used by procedures can be local variables, module variables, or parameters. Parameters are used when calling the procedure in order to pass data and variables to the procedure. The caller passes the arguments to the procedure parameters in the same order they were defined as parameters. The procedure then uses the parameters to perform calculations or any other tasks it needs to perform.

Two types of parameters are available in ATEasy: Val parameters and Var parameters. A Val parameter receives its initial value from the argument passed to the procedure. It can be changed by the procedure; however, that change is reflected only in the procedure while the value of the argument is not changed. Var parameters hold the address of the argument variable passed to the procedure. Any change to the parameter will be reflected in the argument variable.

Local variables are created each time the procedure is called, and their initial value is set before the procedure code is executed.

Your procedure, Average, has two parameters: ad, used to receive the array, and lSize used to tell the procedure how many elements of the array are needed to calculate the average.

Additional procedure variables will be needed to perform the average calculation. These are: d to hold the sum of the array elements and i to be the loop counter. The loop counter is used to iterate through the array in order to sum the value of all array elements.

To create the procedure variables and parameters:

1.      Right-click in the variables pane of the procedure view. (This is the pane with the headings Name, Type, and Description. It appears below the procedure description pane.) Select Insert Parameter/Variable After . A new variable named Variable1 is inserted.

2.      Rename it to ad by typing or pressing F2 (if not already in edit mode) and typing.

3.      Repeat steps one and two, and define the following variables: lSize, d, and i. By now, you should have four variables defined as Val Long.

4.      Right click on the ad parameter and select Properties . Set the following properties:

 

Name

ad

Parameter

Val

Type

Double

Dim

1

Desc

Array to calc average

5.      Repeat step 3 for the lSize parameter as follows:

 

Name

ISize

Parameter

Val

Type

Long

Dim

0

Desc

Number of elements, use the whole array if omitted

Check the Optional check box. This allows the user to pass or not pass an argument here.

Set the initial value from the Value property page for this parameter as -1. If the caller does not provide the argument, the value of lSize will be -1.

6.      Repeat step 3 for the d local variable as follows:

 

Name

d

Parameter

None

Type

Double

Dim

0

Desc

Sum of elements

7.      Enter the following properties for Variable4:

 

Name

i

Parameter

None

Type

Long

Dim

0

Desc

Loop counter