next up previous contents index
Next: Conditional Statements, Branching Up: MIDAS Command Language Previous: DO Loops

  
Local Keywords

Because keywords are implemented as a global data structure, different MIDAS procedures can access the same keyword. This useful feature can cause problems, however, if these keywords just serve as local, temporary variables like the DO variables. Consider the procedures below:

!+
! Example 11, MIDAS procedure exa11.prg
!+
WRITE/KEYWORD N/I/1/1 0
DO N = 1 10
@@ test
ENDDO
!+
! MIDAS procedure test.prg
!+
WRITE/KEYWORD N/I/1/1 0
DO N = 1 12
WRITE/KEYWORD INPUTI/I/12/1
$\{$N$\}$
ENDDO
Executing @@ exa11 will give some unexpected results, since both procedures access the same integer keyword N as a common variable.

Therefore, procedures should use local keywords for DO loops  and internal working storage. Local keywords are defined inside a MIDAS procedure via the command DEFINE/LOCAL. They are only known inside the procedure where they are defined (if the lower_levels_flag is set, they are also defined in all procedures called from this procedure). Local keywords  may have the same name as an existing global keyword (except the system keyword names as stored in MID_MONIT:syskeys.dat) or local keyword of any other procedure, since local keywords are searched before the global ones. The above example will work, if modified as follows:
!+
! Example 12, MIDAS procedure exa12.prg
!+
DEFINE/LOCAL N/I/1/1 0
DO N = 1 10
@@ test
ENDDO
!+
! MIDAS procedure test.prg
!+
DEFINE/LOCAL N/I/1/1 0
DO N = 1 12
WRITE/KEYWORD INPUTI/I/12/1
$\{$N$\}$
ENDDO
Local keywords are deleted when returning to the next higher level at the end of a procedure.
Note
Always provide all initial values to a keyword. For example,
define/local lola/r/1/6 0 usually works as intended, i.e. all elements of keyword lola are filled with 0, but depending upon the computer and operating system the contents of lola(2,...,6) may be unpredictable. So, use
define/local lola/r/1/6 0 all in order to fill all elements of keyword lola with 0.

next up previous contents index
Next: Conditional Statements, Branching Up: MIDAS Command Language Previous: DO Loops
Petra Nass
1999-06-09