Declare Function with passed array

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

Declare Function with passed array

Postby Richard » Apr 25, 2007 3:02

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: 7667
Joined: May 28, 2005 3:28

Postby D.J.Peters » Apr 25, 2007 3:11

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

Postby Richard » Apr 25, 2007 3:31

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: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Apr 25, 2007 10:35

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

Postby Richard » Jul 29, 2007 1:52

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 ?
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Jul 29, 2007 1:56

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: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Jul 29, 2007 2:08

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

Postby Richard » Jul 29, 2007 2:09

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

Postby cha0s » Jul 29, 2007 2:11

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

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest