LOCAL statement

In Decimal BASIC, you can declare local variables in internal procedures using the nonstandard LOCAL statement.
In the section on the key points of Full BASIC (functions and subprograms) , we present the following example, which does not work properly as a program to find an integer solution to the indeterminate equation ax + by = c .

100 INPUT a,b,c
110 WHEN EXCEPTION IN
120    CALL solve(a,x,b,y,c)
130    PRINT x,y
140 USE
150    PRINT "No solution"
160 END WHEN
170 SUB solve(a,x,b,y,c)
180    IF b=0 THEN
190       IF MOD(c,a)=0 THEN
200          LET x=c/a
210          LET y=0
220       ELSE
230          LET x=1/0           ! Raise an exception
240       END IF
250    ELSE
260       LET q=INT(a/b)
270       LET r=MOD(a,b)
280       CALL solve(b,u,r,v,c)
290       LET x=v
300       LET y=u-q*v
310    END IF
320 END SUB 

It will work correctly if you declare all variables used in the subprogram as local variables with:

175    LOCAL q,r,u,v


Note that r does not appear after line 280, so it may seem that it is not necessary to make it a local variable, but line 280 passes each variable by reference, so if you do not make r a local variable, r and b will point to the same variable during the recursive call, and it will not work correctly.
However, if you change line 280 to CALL solve(b,u,(r),v,c) and pass r by value, it will work correctly even if you remove r from the LOCAL statement declaration.

Elimination of LOCAL statements

Although the Full BASIC standard does not include the Local statement, if you only have simple variables, you can eliminate the Local statement by adding all variables you want to localize to the formal arguments of the procedure, and then modifying the procedure so that it calls with appropriate constants (i.e., by making them value arguments).
For example, in the above program, if lines 170, 120, and 280 are chaged as below It will work correctly.

120    CALL solve(a,x,b,y,c,0,0,0,0)
170 SUB solve(a,x,b,y,c,q,r,u,v)
280       CALL solve(b,u,r,v,c,0,0,0,0)


However, it will be difficult to understand, so it is better to avoid it unless your goal is to conform to the standard.


–ß‚é