Author Topic: how to write a macro with optional arguments  (Read 286 times)

0 Members and 1 Guest are viewing this topic.

chrisc

  • Guest
how to write a macro with optional arguments
« on: April 27, 2018, 07:25:52 AM »
Hello all

how to write a macro that passes optional arguments

  for example, the macro to reset several variables to zero where mNum1 is mandatory
  while   mNum2 and mNum3 are optional

Code: [Select]
macro ResetOrdNumber(  mNum1  ,  optional mNum2  , optional  mNum3)
          mNum1 = 0
         
         if exist (mNum2) then mNum2 = 0
       
          if exist(mNum3) then mNum3 = 0

end macro

so we can call the macro to reset several variables

   '   scenario 1
      ResetOrdNumber  anum ,  bnum , cnum



     or  just reset only anum and not declaring bnum , cnum
       since bnum and cnum are optional arguments

  '   scenario 2
      ResetOrdNumber  anum


    runing either scenario 1 or scenario 2 won't result in an error
   
    the aim is to how to make these arguments optional , whereby the programer can
    optionally enter arguments
« Last Edit: April 27, 2018, 08:05:21 AM by chrisc »

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3745
    • Oxygen Basic
Re: how to write a macro with optional arguments
« Reply #1 on: April 28, 2018, 04:44:26 AM »
Hi Chris,

This is the way to handle optional arguments:

Code: [Select]
macro ResetNum(A,B,C,D)
#ifdef A
  A=0
#endif
#ifdef B
  B=0
#endif
#ifdef C
  C=0
#endif
#ifdef D
  D=0
#endif
end macro

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3745
    • Oxygen Basic
Re: how to write a macro with optional arguments
« Reply #2 on: April 28, 2018, 05:19:34 AM »
But there is an easier way :) I've made a small change to Oxygen.dll making this possible:

Repeaters:

%* ResetNum %1=0  :
%* ResetStr %1=""  :


Code: [Select]
%* ResetNum %1=0  :
%* ResetStr %1="" :
int a,b,c,d
string s,t,u,v
ResetNum a,b,c,d
ResetStr s,t,u,v

https://github.com/Charles-Pegge/OxygenBasic/blob/master/OxygenProgress.zip

Charles Pegge

  • Admin Support Member
  • *****
  • Posts: 3745
    • Oxygen Basic
Re: how to write a macro with optional arguments
« Reply #3 on: April 28, 2018, 09:45:21 AM »
This is where macros get complicated, ..well just a bit:

This macro combination will work for non-dynamic arrays of strings and numbers.
Code: [Select]
macro ResetArrayMacro(A,   i)
=============================
scope
  indexbase 1
  int i
  for i=1 to countof A
    #if typecodeof A < 0x80 'numbers
      A[i]=0
    #elseif typecodeof A < 0x100 'strings
       A[i]=""
    #endif
  next
end scope
'line spacer
end macro

%* ResetArray ResetArrayMacro(%1) :

'TEST
int a[10],b[10]={1,2,3,4}
string s[10],t[10]={"W","X","Y","Z"}
ResetArray a,b
ResetArray s,t
print t[1] t[2] b[1] b[2]