Can functions return arrays?

New to FreeBASIC? Post your questions here.
evil_twin
Posts: 3
Joined: Jan 14, 2015 20:39

Can functions return arrays?

Post by evil_twin »

Hi, I'm fairly new to FreeBasic (after a few decades of absence from PowerBasic) and spoiled by PHP. I've been looking through the manual and couldn't find out if a function can return an array, even if it can be passed one. Any ideas please?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Can functions return arrays?

Post by vdecampo »

You you can pass a pointer to the array. The biggest issue is there is no mechanism to dynamically declare an array based on the return from a function (That I know of). For instance in C# you can do...

Code: Select all

string[] parts = somestring.Split(',');
This would declare a string array of parts based on the return of the Split() function. This doesn't exist in FB.

What you can do is...

Code: Select all

Sub DoSomething(arry As Integer Ptr, maxidx As Integer)

	For x As Integer = 0 To maxidx
		arry[x]= Rnd*10
	Next

End Sub

Dim myarray(10) As Integer

DoSomething(@myarray(0), UBound(myarray))

For x As Integer = 0 To UBound(myarray)
	Print myarray(x)
next

Sleep
-Vince
fxm
Moderator
Posts: 12153
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can functions return arrays?

Post by fxm »

One can also simply pass the array as an argument (in fact, only its descriptor is passed by reference) instead of passing a pointer to its first element:

Code: Select all

Sub DoSomething(array() As Integer)

   For x As Integer = 0 To Ubound(array)
      array(x)= Rnd*10
   Next

End Sub

Dim myarray(10) As Integer

DoSomething(myarray())

For x As Integer = 0 To UBound(myarray)
   Print myarray(x)
next

Sleep
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

would be nice if they could integrate a help function within an editor that's currently out there for FB, the best resource is here...

http://www.freebasic.net/wiki/wikka.php ... gFullIndex


the whole purpose of fb was to be as strong and versatile as the best platforms available, AND use QB's ease of programming.

where the problem comes in is the original QB help files or KEYWORD LIST isn't really attached in any way to the assembler,
other than BEING ABLE TO USE ALMOST EVERY SINGLE WORD OF CODE FROM QB!!!! HAHAHAHA
I do recall a certain version of FBIDE that had included it, but mine isn't working.....

BUT TO THE POINT!!!

You can pass arrays directly to video chipset using QB like SYNTAX with a few lines of code, thanks to all the hard work of FB's developers,
the versatility of FB and its ability to use current and future library's is AMAZING to say the least.

I have always hated functions and subs....hahaha why does a program need to bounce around, wheres the FLOW in that...hahaha

just use 'gosub'....till you learn all the wonderful many,MANY mechanisms.
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Can functions return arrays?

Post by jevans4949 »

@evil_twin:
In case it's what you need to do, you can declare a dynamic array using ReDim, and resize it in your function using ReDim again.

Trival demo:

Code: Select all

Sub resizer(hisarray()As integer)
  ReDim hisarray(0 To 99)
  hisarray(99) = 42
End Sub

reDim myarray(0) As Integer
resizer(myarray())
Print LBound(myarray,1),UBound(myarray,1),myarray(UBound(myarray,1))
sleep
End
(Had to check it out as I hadn't tried before.)
evil_twin
Posts: 3
Joined: Jan 14, 2015 20:39

Re: Can functions return arrays?

Post by evil_twin »

Thanks for the replies. In the end I just plonked in some inline code. Not as elegant but will run faster that way. As for GOTOs and GOSUBs, I understand they can be much more efficient then procedures and such like, but you need a lot of discipline to keep your code tidy ...
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Can functions return arrays?

Post by sancho2 »

There is also the option of wrapping the array in a type and returning that:

Code: Select all

Type MyArray
	As Integer n(1 To 10)
End Type
Function test() As MyArray
	Dim As MyArray t
	For x As Integer = 1 To 10
		t.n(x) = x
	Next
	Return t
End Function

Dim p As MyArray
p = test()

For x As Integer = 1 To 10
	Print p.n(x)
Next

Sleep
evil_twin
Posts: 3
Joined: Jan 14, 2015 20:39

Re: Can functions return arrays?

Post by evil_twin »

sancho2 wrote:There is also the option of wrapping the array in a type and returning that:
Thanks - this is probably what I was looking for. at least there is a workaround ...
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

sancho2 wrote:There is also the option of wrapping the array in a type
Or something like that? (a bit more esotheric)

Code: Select all

type ARR
    as any ptr  _addressOfLowerBoundElement
    as integer  _sizeOfOneElement
    as integer  _arrayRange
end type

function ArrayReturner( ArrayExample() as integer ) as ARR
    dim as ARR  returnValue
    returnValue._addressOfLowerBoundElement = @ArrayExample(lBound(ArrayExample))
    returnValue._sizeOfOneElement = sizeOf(ArrayExample(lBound(ArrayExample)))
    '
    return returnValue
end function

dim as integer  someArray(1 to 9) = {1,2,3,4,5,6,7,8,9}

? *cast( _
        integer ptr, _ 
        ArrayReturner( someArray() )._addressOfLowerBoundElement + 4*ArrayReturner( someArray() )._sizeOfOneElement _ 
        )


sleep
'(eof)

Or, more general:

Code: Select all

type ARR
    as any ptr  _addressOfLowerBoundElement
    as integer  _sizeOfOneElement
    as integer  _arrayRange
end type


dim as string  someArray(1 to 9) = {"a","b","c","d","e","f","g","h","i"}


function ArrayReturner( ArrayExample() as typeOf(someArray) ) as ARR
    dim as ARR  returnValue
    returnValue._addressOfLowerBoundElement = @ArrayExample(lBound(ArrayExample))
    returnValue._sizeOfOneElement = sizeOf(ArrayExample(lBound(ArrayExample)))
    '
    return returnValue
end function


? *cast( typeOf(someArray) ptr , _ 
        cast( _
            any ptr, _ 
            ArrayReturner( someArray() )._addressOfLowerBoundElement + 4*ArrayReturner( someArray() )._sizeOfOneElement _ 
            ) _ 
        )


sleep
'(eof)

But; this, more general, is not allowed (based on "function ArrayReturner( ArrayExample() as typeOf(ArrayExample) ) as ARR")

Code: Select all

type ARR
    as any ptr  _addressOfLowerBoundElement
    as integer  _sizeOfOneElement
    as integer  _arrayRange
end type


dim as string  someArray(1 to 9) = {"a","b","c","d","e","f","g","h","i"}


function ArrayReturner( ArrayExample() as typeOf(ArrayExample) ) as ARR
    dim as ARR  returnValue
    returnValue._addressOfLowerBoundElement = @ArrayExample(lBound(ArrayExample))
    returnValue._sizeOfOneElement = sizeOf(ArrayExample(lBound(ArrayExample)))
    '
    return returnValue
end function


? *cast( typeOf(someArray) ptr , _ 
        cast( _
            any ptr, _ 
            ArrayReturner( someArray() )._addressOfLowerBoundElement + 4*ArrayReturner( someArray() )._sizeOfOneElement _ 
            ) _ 
        )


sleep
'(eof)
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

the whole and sole purpose of a function is to be able to pass variables to and from...and perform an operation within said function...

is this a trick question??? LOL

I'm assuming the example of creating a library in FB that's included in the package works....it actually makes a library that is a function, that you pass variables to, and arrays can be passed the exact same way.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

thesanman112 wrote: is this a trick question??? LOL
I think the question is more about procedures. Otherwise we could think also of macros or many other black boxes, but it would be maybe too much general, I don't know. Maybe you could show an example with a library returning an array?
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

I'm still trying to get the example that makes a library to work...seems to not want to build the correct files...

I got the library compiling now,
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Can functions return arrays?

Post by TeeEmCee »

To extend sancho2's example, note that you can also put a dynamic length array in a Type. Declare it like "As Integer n(any)", or "As Integer n(any, any)" for a two-dimensional array, etc.
Tourist Trap wrote:But; this, more general, is not allowed (based on "function ArrayReturner( ArrayExample() as typeOf(ArrayExample) ) as ARR")
Your last two code blocks appear to be identical. What did you mean?
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

Tourist Trap wrote:
thesanman112 wrote: is this a trick question??? LOL
I think the question is more about procedures. Otherwise we could think also of macros or many other black boxes, but it would be maybe too much general, I don't know. Maybe you could show an example with a library returning an array?

mylib.bi

Code: Select all

declare function ArrayofNumbers alias "ArrayofNumbers" ( x as integer, a() as integer,b as integer) as integer 
mylib.bas

Code: Select all

''
'' mydll -- simple dll test
''
'' compile as: fbc -dll mydll.bas

#include once "mydll.bi"

function ArrayofNumbers(x as integer, a() as integer,b as integer) as integer export
	dim as integer z
    for z=1 to x
    function = a(z)+b:print a(z)+b
    next z
end function
dylib.bas

Code: Select all


dim library as any ptr

dim ArrayofNumbers as function( x as integer, a() as integer,b as integer) as integer

library = dylibload( "mydll" )
if( library = 0 ) then
	print "Cannot load the mydll dynamic library"
	sleep
	end 1
end if

ArrayofNumbers = dylibsymbol( library, "ArrayofNumbers" )
if( ArrayofNumbers = 0 ) then'
	print "Could not get arrayofnumbers address from mydll library"
	end 1
end if

randomize( timer( ) )

dim as integer x,b,v
dim as integer a(10)
for x=1 to 10
    a(x)=int(rnd*20):print a(x)
next x
print"*****"
x=10
b=5

v=arrayofnumbers(x,a(),b)

sleep
dylibfree library

thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

as you can tell the entire array is passed to the function correctly, however, returning back from the function the array remains unchanged.
Post Reply