Can functions return arrays?
Can functions return arrays?
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?
Re: Can functions return arrays?
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...
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...
-Vince
Code: Select all
string[] parts = somestring.Split(',');
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
Re: Can functions return arrays?
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
-
- Posts: 538
- Joined: Jul 15, 2005 4:13
Re: Can functions return arrays?
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.
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.
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Re: Can functions return arrays?
@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:
(Had to check it out as I hadn't tried before.)
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
Re: Can functions return arrays?
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 ...
Re: Can functions return arrays?
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
Re: Can functions return arrays?
Thanks - this is probably what I was looking for. at least there is a workaround ...sancho2 wrote:There is also the option of wrapping the array in a type and returning that:
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Can functions return arrays?
Or something like that? (a bit more esotheric)sancho2 wrote:There is also the option of wrapping the array in a type
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)
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)
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)
-
- Posts: 538
- Joined: Jul 15, 2005 4:13
Re: Can functions return arrays?
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.
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.
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Can functions return arrays?
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 wrote: is this a trick question??? LOL
-
- Posts: 538
- Joined: Jul 15, 2005 4:13
Re: Can functions return arrays?
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,
I got the library compiling now,
Re: Can functions return arrays?
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.
Your last two code blocks appear to be identical. What did you mean?Tourist Trap wrote:But; this, more general, is not allowed (based on "function ArrayReturner( ArrayExample() as typeOf(ArrayExample) ) as ARR")
-
- Posts: 538
- Joined: Jul 15, 2005 4:13
Re: Can functions return arrays?
Tourist Trap wrote: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 wrote: is this a trick question??? LOL
mylib.bi
Code: Select all
declare function ArrayofNumbers alias "ArrayofNumbers" ( x as integer, a() as integer,b as integer) as integer
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
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
-
- Posts: 538
- Joined: Jul 15, 2005 4:13
Re: Can functions return arrays?
as you can tell the entire array is passed to the function correctly, however, returning back from the function the array remains unchanged.