return a function with array

General FreeBASIC programming questions.
Post Reply
Raddel
Posts: 23
Joined: Oct 07, 2007 3:31

return a function with array

Post by Raddel »

can I return some function with array variable

sample coding:
declare function getData() as <???>

...
...

function getData() as <???>
dim I as integer
dim datas(5) as integer

for I = 1 to ubound(datas)
datas(I)=I
next

return datas
end function


how i can fill type of data for this function?
correct me if this function is false
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

Code: Select all

Sub SomeFunc( Byref Array() As SomeType )
   
   For I As Integer = lBound( Array ) To uBound( Array )
      Array( I ) = SomeValue
   Next
   
End Sub


Dim As SomeType   foo( 0 To 9 )
Dim As SomeType   bar( -100 To 99 )

SomeFunc( foo )
SomeFunc( bar )
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

You can't directly return arrays with functions. You have to pass the array as a parameter, then let the sub modify it that way.
I'll post another example, because 1000101's has a couple of syntax errors:

Code: Select all

sub getData( datas() as integer )
    
    redim datas(5) as integer
    
    for I as integer = lbound(datas) to ubound(datas)
        datas(I) = I
    next
    
end sub

dim datas() as integer
getData( datas() )

for I as integer = lbound(datas) to ubound(datas)
    print datas(I)
next
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

Mine doesn't have any syntax errors if you replace the obvious keywords to be replaced with appropriate values for your application. Mine wasn't meant to be a "working example" but a "general idea example."
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

1000101 wrote:Mine doesn't have any syntax errors if you replace the obvious keywords to be replaced with appropriate values for your application.
Those aren't syntax errors. The problems I was referring to are:
- You can't use the ByRef (or ByVal) keyword when declaring an array parameter.
- When passing an array as a parameter, you have to put a pair of brackets "()" after it.
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Post by VirusScanner »

I added this to the sourceforge tracker a little while ago ( http://sourceforge.net/tracker/index.ph ... tid=693199 ) but as there is an easy workaround already, it's not top priority.
Post Reply