Author Topic: Array as parameter question  (Read 1654 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Guest
Array as parameter question
« on: November 20, 2018, 04:15:28 PM »
 I am still not very experienced with Oxygen... i was wondering is there is already
a way to pass an array created on the fly as parameter, kind of like this:

Code: [Select]
CALL SomeFunction(new int{0, 1, 2, 3, 4})
If yes, what would be the complete way to do it?

 Thanks!

Brian Alvarez

  • Guest
Re: Array as parameter question
« Reply #1 on: November 20, 2018, 04:18:36 PM »
What i am trying to do is to be able to pass an undefined (infinite if possible) number of
integers to a function without having to define them all as optional. If there is another
way to do it without the array "hack" would be even better.

Charles Pegge

  • Guest
Re: Array as parameter question
« Reply #2 on: November 20, 2018, 04:37:17 PM »

Yes, you can pass sets of data in anonymous arrays, and also pass the element count with countof

Code: [Select]
'PASSING ANONYMOUS ARRAYS

function f(int *a,n)
print n
int i
for i=1 to n
  a[i]*=2
next
end function

f( int{1,2,3,4,5}, countof )

Brian Alvarez

  • Guest
Re: Array as parameter question
« Reply #3 on: November 20, 2018, 08:55:08 PM »
Awesome! Thanks charles.

Arnold

  • Guest
Re: Array as parameter question
« Reply #4 on: November 22, 2018, 12:41:09 AM »
Hi Charles,

I often find the situation that structures are passed as parameters. This is a (very simplified) example:

Code: [Select]
uses console

type RECT
  long left
  long top
  long right
  long bottom
end type


function f(sys *prc) as *sys 'RECT ptr
   RECT *rc : &rc=&prc
   
   printl "in function f: " rc.left ", " rc.top ", "rc.right ", "rc.bottom
   return &rc
end function

RECT ptRect1 = {10,15,100,200}
RECT *ptRect2

&ptRect2 = f(&ptRect1)
printl "ptRect = " ptRect2.left ", " ptRect2.top ", "ptRect2.right ", "ptRect2.bottom

printl "Enter: " : waitkey

The example works, but perhaps there is a better way to do this?

Roland

JRS

  • Guest
Re: Array as parameter question
« Reply #5 on: November 22, 2018, 02:21:08 AM »
At the risk of abuse from the anti-SB members on this forum, here is a DLLC example of what I think you are trying to get working. You would have to look at the O2 DLLC code to see how Charles implement this feature in DLLC.

Code: Script BASIC
  1. 'CREATING A COMPOUND TYPE
  2.  
  3. rectangle=dlltype("rectangle (i left,i top, i right, i bottom)")
  4. print dllreco(rectangle) & "\n"
  5.  
  6. SmallRectangle=dlltype("SmallRectangle (s left,s top, s right, s bottom)")
  7. print dllreco(rectangle) & "\n"
  8.  
  9. 'CREATING A COMPOUND VARIABLE (ARRAY OF 2 UNITS)
  10.  
  11. rect=dlldimv(rectangle,2)
  12.  
  13. print "Length of rect variables record: (16+4*2) " & len(rect) & "\n"
  14.  

Arnold

  • Guest
Re: Array as parameter question
« Reply #6 on: November 22, 2018, 02:40:03 AM »
Hi John,

the code which I currently try to port, uses a RECT type in a function. This type and some other values are manipulated in several additional functions. Some functions only transport this RECT type to the next function / sub without changing the values. It is a bit difficult to follow the flow, but it is possible. I am just wondering if my way (trial and error) is right or if there is a better (logical) solution.

Roland
 

Aurel

  • Guest
Re: Array as parameter question
« Reply #7 on: November 22, 2018, 02:40:34 AM »
Quote
but perhaps there is a better way to do this?

Hi A

I am not sure is there "a better way" but i am sure that can be in different way but in end all
is the same ...
UDT members are parameters or arguments of this structure and as such give us another level of indirections to
specific variable..
uf look at me  i am babeling.
I don't know whst is wrong with for example rc.top or rc,buttom
i simply dont have problem with such a things if work properly.

Charles Pegge

  • Guest
Re: Array as parameter question
« Reply #8 on: November 22, 2018, 05:39:30 AM »
Hi Roland,

This should work on your o2 version:

Code: [Select]
uses console

type rect
  long left
  long top
  long right
  long bottom
end type


function f(rect*rc) as rect*
   'rect rc at prc
   printl "in function f: " rc.left ", " rc.top ", "rc.right ", "rc.bottom
   return &rc
end function

RECT Rect1 = {10,15,100,200}
RECT *ptRect2
&ptRect2 = f(Rect1)
&ptRect2 = f(rect{10,15,100,200}) 'anon rect
printl "ptRect = " ptRect2.left ", " ptRect2.top ", "ptRect2.right ", "ptRect2.bottom

printl "Enter: " : waitkey


Brian Alvarez

  • Guest
Re: Array as parameter question
« Reply #9 on: November 22, 2018, 09:03:00 AM »
I Will be using this trick as well.  ;D