Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
I defined a Structure with a large number of members. Some of the members include arrays. I defined a separate function to operate on an array, but don't want to pass the entire Structure to preserve speed. Is this possible? The following example fails to compile:
option base 1
Structure bar
Dim i as Integer
Dim a(1 to 3) as Integer
End Structure
Sub Main()
Dim b1 as bar
Dim i as Integer
i = foo(b1.a)
debug.print "i=" & i
End Sub
Function foo(ByRef b() as Integer) as Integer
foo = b(1)+b(2)+b(3)
End Function
It looks like the compiler does have a problem with this code. The following might be a possible workaround. Note that it is untested as I do have a ZX device with me right now.
Sub Main()
Dim b1 as bar
Dim i as Integer
i = foo(MemAddressU(b1.a))
Debug.Print "i=" & i
End Sub
Function foo(ByVal addr as UnsignedInteger) as Integer
Dim b() as Integer Based addr
foo = b(1)+b(2)+b(3)
End Function
liam.zbasic wrote: don't want to pass the entire Structure to preserve speed.
It makes no difference. Passing an array and a structure are both done using a reference behind the scenes, whether ByRef or ByVal. The code below compiles without error.
option base 1
Structure bar
Dim i as Integer
Dim a(1 to 3) as Integer
End Structure
Sub Main()
Dim b1 as bar
Dim i as Integer
i = foo(b1)
debug.print "i=" & i
End Sub
Function foo(ByRef b as bar) as Integer
foo = b.a(1)+b.a(2)+b.a(3)
End Function
liam.zbasic wrote:The following example fails to compile:
This is a known problem that has already been corrected. We are in the final stages of preparing a new release.