LB Booster
« Passing Arrays »

Welcome Guest. Please Login or Register.
Apr 1st, 2018, 05:09am



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 2  Notify Send Topic Print
 hotthread  Author  Topic: Passing Arrays  (Read 3500 times)
bluatigro
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 111
xx Re: Passing Arrays
« Reply #21 on: Feb 17th, 2015, 09:56am »


pasing arrays into subs and functions
ok thats nice

but how do i make a function that outputs a array ?
User IP Logged

Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 1348
xx Re: Passing Arrays
« Reply #22 on: Feb 17th, 2015, 11:47am »

on Feb 17th, 2015, 09:56am, bluatigro wrote:
but how do i make a function that outputs a array ?

You pass the array BYREF, as follows:

Code:
    call mySub myArray()
    print myArray(50)
    end

sub mySub byref a()
    dim a(100)
    a(50) = 12345
end sub 

Here myArray() does not yet exist when the sub is called; the local array a() is created inside the sub and then passed back to the caller as myArray(). On return from the sub a() ceases to exist.

LB 4 doesn't have any functionality like this; arrays are always global so it is impossible to write a general-purpose function which operates on any specified array. This is in my opinion a major weakness.

Richard.
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #23 on: Mar 1st, 2015, 03:07am »

Just wanted to follow up and say thanks for your help. The ability to re-dimension arrays and preserve their contents is very useful. smiley
User IP Logged

JosephE
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 35
xx Re: Passing Arrays
« Reply #24 on: Mar 1st, 2015, 03:36am »

In fact, here's an up-to-date version of the code that supports scaling of string and numeric arrays:

Code:
' Simple Array Functions
' by Joseph Essin and Richard Russell
'
' For use with LBB:
' http://www.bbcbasic.co.uk/lbb/
'
' The functionality to make this work is not found in
' Liberty BASIC alone. This is for use with Richard Russell's
' Liberty BASIC Booster "compiler." Richard even customized LBB
' to allow these user-defined functions to work.
'
' Please find LBB below:
' http://www.bbcbasic.co.uk/lbb/
' -------------------------------------------------------------- '

' -------------------------------------------------------------- '
' These are necessary for the Array Library.
' Don't name any of your arrays these names
' or you'll be subject to data loss.
redim ArrayLib(0)
redim ArrayLib$(0)
' -------------------------------------------------------------- '
function nsize(array())
  ' Returns the upper index limit of the numeric array array().
  s = 2^24
  do
    on error goto [Error]
    [Error]
    s = int(s / 2)
    temp = array(nsize + s)
    nsize = nsize + s
  loop until s = 0
end function

function size(array$())
  ' Returns the upper index limit of the string array array$().
  s = 2^24
  do
    on error goto [Error]
    [Error]
    s = int(s / 2)
    temp$ = array$(size + s)
    size = size + s
  loop until s = 0
end function

sub preserve byref array(), newSize
	' Redimensions the given one-dimensional numeric array to the
	' specified newSize.
	u = nsize(array()) ' Get the upper limit
	redim ArrayLib(u)
	for i = 0 To u
    ArrayLib(i) = array(i) ' Get old contents
	next i
	redim array(newSize) ' Resize given array.
	for i = 0 To u
    array(i) = ArrayLib(i) ' Replace old contents
	next i
end sub

sub preserve$ byref array$(), newSize
	' Redimensions the given one-dimensional string array to the
	' specified newSize.
	u = size(array$()) ' Get the upper limit
	redim ArrayLib$(u)
	for i = 0 To u
    ArrayLib$(i) = array$(i) ' Get old contents
	next i
	redim array$(newSize) ' Resize given array.
	for i = 0 To u
    array$(i) = ArrayLib$(i) ' Replace old contents
	next i
end sub 
« Last Edit: Mar 1st, 2015, 03:38am by JosephE » User IP Logged

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


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