return a function with array

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

return a function with array

Postby Raddel » Oct 28, 2007 14:36

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

Postby 1000101 » Oct 28, 2007 14:54

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: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Oct 28, 2007 16:16

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

Postby 1000101 » Oct 29, 2007 0:06

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: 6221
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Oct 29, 2007 13:17

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

Postby VirusScanner » Oct 30, 2007 3:14

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.

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests