Declare Function with passed array

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

Declare Function with passed array

Post by Richard »

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 Double
Function one (x As Double) As Double
    one = x
End Function
Print one(1)

' Declare Function two(As Integer, As Double) As Double

Function two ( n As Integer, x() As Double) As Double
    two = x(n)
End Function

Dim As Double x2(3)
Dim As Integer nn = 2
x2(nn) = 2
Print two( nn, x2(nn) )

Sleep
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

Code: Select all

Declare Function one(x As Double) As Double
Declare Function two(x() As Double,n As Integer) As Double

Function one (x As Double) As Double
    one = x
End Function
Print one(1)


Function two (x() As Double,n as integer) As Double
    two = x(n)
End Function

Dim As Double x2(3)
Dim As Integer n = 2
x2(n) = 2
Print two(x2(n),n)

Sleep
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

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
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

You can also write it like "two(x2(), n)". I'm honestly surprised that it accepts an indexed array as an arg.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

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 specification
Declare 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 ?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

ByRef Arrays = Surprise to me. They're ByDesc, aren't they? They've always been. I never heard about such a change!
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

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: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

But there is no ByDesc keyword available!
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

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! ;)
Post Reply