## Declare Function with passed array

New to FreeBASIC? Post your questions here.
Richard
Posts: 2953
Joined: Jan 15, 2007 20:44
Location: Australia

### Declare Function with passed array

I am having trouble finding out how to write a "Declare Function" statement that specifies an array as a passed parameter. The following code runs ok but if I uncomment the "Declare Function two(... " line it gives me "error 55, type missmatch" at the "Function two(..." line later on.
How should I write the "Declare Function two(... )" line?

Code: Select all

`Declare Function one(As Double) As DoubleFunction one (x As Double) As Double    one = xEnd FunctionPrint one(1)' Declare Function two(As Integer, As Double) As DoubleFunction two ( n As Integer, x() As Double) As Double    two = x(n)End FunctionDim As Double x2(3)Dim As Integer nn = 2x2(nn) = 2Print two( nn, x2(nn) )Sleep`
D.J.Peters
Posts: 7808
Joined: May 28, 2005 3:28

Code: Select all

`Declare Function one(x As Double) As DoubleDeclare Function two(x() As Double,n As Integer) As DoubleFunction one (x As Double) As Double    one = xEnd FunctionPrint one(1)Function two (x() As Double,n as integer) As Double    two = x(n)End FunctionDim As Double x2(3)Dim As Integer n = 2x2(n) = 2Print two(x2(n),n)Sleep`
Richard
Posts: 2953
Joined: Jan 15, 2007 20:44
Location: Australia
D.J.Peters, Thanks for that. To put the empty brackets in to specify an array needs any dummy variable name. The word array seems to work and makes the meaning obvious.
So simple.

Code: Select all

`Declare Function two( As Integer, array() As Double) As Double`
cha0s
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:
You can also write it like "two(x2(), n)". I'm honestly surprised that it accepts an indexed array as an arg.
Richard
Posts: 2953
Joined: Jan 15, 2007 20:44
Location: Australia
Since arrays are now passed Byref and might one day be passed Byval, I am being explicit by declaring:

Code: Select all

`Declare Function x ( Byref array() As Integer) as Integer' the above gives a compile error 56 - illegal specificationDeclare Function x ( array() As Integer) as Integer ' but this works OK `
Is it possible to allow me to be explicit when passing arrays to functions and subs ?
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA
ByRef Arrays = Surprise to me. They're ByDesc, aren't they? They've always been. I never heard about such a change!
cha0s
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:
Yeah, they aren't passed byref as other variables are, a reference to their descriptor is passed so it's more like "bydesc". Been that way since FB was created, as far as I know.
Richard
Posts: 2953
Joined: Jan 15, 2007 20:44
Location: Australia
But there is no ByDesc keyword available!
cha0s
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:
That's because FreeBASIC keeps the dirty details hidden from you. :P

It would be nice to have the option to pass arrays byval, but that would be slow as \$%#@ for huge arrays.

Next thing you know, people will ask to treat byref variables like the pointers they really are! ;)