Specifying Default Arrays on Function Headers

General FreeBASIC programming questions.
Post Reply
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Specifying Default Arrays on Function Headers

Post by jevans4949 »

Is dementia setting in?

The following code compiles OK

Code: Select all

Dim x(0 To 255) As LongInt
Function y (z As Long, a() As LongInt) As Integer
	Function = 0
End Function
The following throws error 58: Illegal specification, at parameter 2 (a) of y() in 'Function y ...

Code: Select all

Dim x(0 To 255) As LongInt
Function y (z As Long, a() As LongInt = x()) As Integer
	Function = 0
End Function
The objective is to pass an array to the function y, but if the user doesn't specify, to use a default. For the planned code to work it's essential to pass an array, to be able to use array functions

Tried several variations of the phrase "= x()" but can't get it right. Or can't I do this at all?
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Specifying Default Arrays on Function Headers

Post by sancho3 »

Workaround:
Overload the function.
Don't forget to make x shared so it can be used inside the functions.

Code: Select all

Dim Shared x(0 To 255) As LongInt
Function y overload (z As Long, a() As LongInt) As Integer
   Function = 0
End Function
Function y Overload(z As long) As Integer 
	Function = y(z, x())
End Function
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Specifying Default Arrays on Function Headers

Post by jevans4949 »

Thanks, @sancho3, I'll give it a spin. (My example was simplified).)
Post Reply