Author Topic: Passing data directly to a function  (Read 1996 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Passing data directly to a function
« on: October 22, 2014, 09:38:48 PM »
By using a macro which creates a scoped array in which to pass the data.


DataCall fun points( {"one",1,2,3},{"two",2,4,6} )

def DataCall
  '%1 function name
  '%2 variable type
  '%3 data

  scope
    %2 d = { %3 }
    %1 d, countof d
  end scope
end def



Code: [Select]

include "$/inc/console.inc"
'indexbase 0

def DataCall
  '%1 function name
  '%2 variable type
  '%3 data
  scope
    %2 d = { %3 }
    %1 d, countof d
  end scope
end def


type points
  string name
  double x,y,z
end type

function fun(Points*d, sys n)
  for i=1 to n
   printl d[i].name tab d[i].x tab d[i].y tab d[i].z
  next
end function

'scope
'Points d= { {"one",1,2,3},{"two",2,4,6} } : fun d, countof d
'end scope

'using the macro:
DataCall fun points( {"one",1,2,3},{"two",2,4,6} )


waitkey

Mike Lobanovsky

  • Guest
Re: Passing data directly to a function
« Reply #1 on: October 23, 2014, 08:06:03 AM »
Nice find, Charles.

In FBSL, braces define an anonymous (i.e. nameless) array of temp Variant vars, and such function calls are possible without the use of a macro or UDT definition. The array would provide the usual LBound(), UBound() and Count() functionality, and GetType() would supply the type info about the array's constituent elements:

Code: [Select]
Sub Foo(a = {}, b = {})
  If Count(a) Then Print a[0]
  If UBound(b) Then Print b[0] + b[1]
End Sub

Foo({!1, !!2, %3, %%4, "five"}) // will print 1.000000 only, won't print anything for b[]

It's great to see minds working in parallel. Perhaps that's exactly why I came here, if anyone wondered. :)

Charles Pegge

  • Guest
Re: Passing data directly to a function
« Reply #2 on: October 23, 2014, 09:22:55 AM »

Hi Mike,

Yes, that highlights the difference in statefulness between compiler and interpreter. Oxygen might benefit from a standard array object, and set of macros to manage data input, multidimensioning, redimesioning, UBOUND & LBOUND.

There are classes called StringArray and TextArray in inc/StringUtil.inc which can be deployed for line-based text. They do not depend on fixed-length arrays.