LB Booster
« Faking a local array »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 03:30am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
We apologize Conforums does not have any export functions to migrate data.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

Thank you Conforums members.
Speed up Liberty BASIC programs by up to ten times!
Compile Liberty BASIC programs to compact, standalone executables!
Overcome many of Liberty BASIC's bugs and limitations!
LB Booster Resources
LB Booster documentation
LB Booster Home Page
LB Booster technical Wiki
Just BASIC forum
BBC BASIC Home Page
Liberty BASIC forum (the original)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Faking a local array  (Read 268 times)
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Faking a local array
« Thread started 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.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls