LB Booster
Programming >> Language extensions >> Faking a local array
http://lbb.conforums.com/index.cgi?board=extensions&action=display&num=1430739738

Faking a local array
Post by Richard Russell on May 4th, 2015, 11:42am

It's an unfortunate limitation of LB 4 that arrays are always global - a characteristic not shared by any other modern BASIC dialect, as far as I know. This means that you cannot write a general-purpose SUB or FUNCTION which uses a temporary, local, array for its own internal purposes.

Necessarily, for reasons of compatibility, LBB shares the characteristic that arrays are always global. However because, in LBB, you can pass an array as a parameter to a SUB or FUNCTION there is a workaround. By passing a dummy array as a parameter, you can create what to all intents and purposes is a local array!

Here is a (rather contrived) example. The function median() returns the median value of a set of 101 random numbers:

Code:
    PRINT median(dummy())
    END

FUNCTION median(array())
    DIM array(100)
    FOR i = 0 TO 100
      array(i) = RND(1)
    NEXT
    SORT array(), 0, 100
    median = array(50)
    REDIM array(0)
END FUNCTION 

The parameter dummy() serves no purpose other than to force array() to be local to the function. However beware the 'gotcha' that the REDIM in the final line of the function is necessary to avoid the memory used by the array being leaked.

Richard.