Using Subroutines
When the subroutine SQUARE is called with a variable as a parameter (in this case, X), a series of
events occurs.
!
!
!
However, if a constant value is passed to the routine, a reassignment cannot be made. For example:
CALL 'SQUARE' (5)
This will assign the value 5 to the parameter variable NUMBER in the subroutine, and NUMBER
will be squared. However, Axcess cannot take the value in NUMBER and reassign it to the constant
value 5. A routine in which a parameter is expected to return a result should not be called with a
constant value as that parameter. To use a constant value as a parameter to this subroutine, and still
get a new value returned, rewrite the DEFINE_CALL to have two parameters, as shown below:
DEFINE_CALL 'SQUARE' (NUMBER,RESULT)
{
}
Here are some examples of using this subroutine:
CALL 'SQUARE' (5,X)(* on return, X will contain 25 *)
CALL 'SQUARE' (X,Y)(* on return, X will contain 25, Y will contain 625 *)
It is important to note that the reassignment of the parameter variable to the caller's variable
happens after the subroutine is finished and before any other code is executed. This could lead to a
hard-to-find bug:
DEFINE_VARIABLE
GLOBAL
DEFINE_CALL 'BUGGY' (PARAM)
{
}
DEFINE_PROGRAM
GLOBAL = 5
CALL 'BUGGY' (GLOBAL)
In this example, GLOBAL contains the value 1Ø at the comment statement. In the subroutine
BUGGY, after GLOBAL is set to 2Ø, Axcess reassigns the value in PARAM to the variable in the
CALL statement which called BUGGY-in this case, GLOBAL. The parameter variable PARAM
contains the value 1Ø at the end of the subroutine, and this value is reassigned to the variable in the
CALL statement GLOBAL.
92
The value of the variable (in this case, 5) is assigned to NUMBER.
The subroutine takes the parameter, multiplies it by itself (squaring it) and assigns the
result back to itself.
Axcess then reassigns NUMBER back to the variable X, so that upon return from the
subroutine, X will contain its previous value squared.
RESULT = NUMBER * NUMBER
PARAM = 1Ø
GLOBAL = 2Ø
(* What will GLOBAL contain here? *)
Axcess Programming Language
Need help?
Do you have a question about the AXCESS CONTROL SYSTEM PROGRAM and is the answer not in the manual?