Author Topic: Functions Example  (Read 5201 times)

0 Members and 2 Guests are viewing this topic.

kryton9

  • Guest
Functions Example
« on: June 16, 2011, 05:17:26 AM »
Code: OxygenBasic
  1. function half( double amount ) as double
  2.         return  amount / 2 'you can use return as shown here, the c way
  3. end function
  4.  
  5. function qrtr( double amount ) as double
  6.         function = amount / 4 'you can return this way, the traditional way
  7. end function
  8.  
  9. 'to use functions that are defined below the program code
  10. 'you need to declare it before the main program code
  11. 'as shown with this next function
  12. declare function tenth( double amount ) as double
  13.  
  14. 'Main Program
  15. dim debt as double = 14432360075155.42
  16.  
  17. print "The U.S. debt as of 6/16/2011  8:45 A.M. = " debt
  18. print "half of that is still so huge: " str( half( debt ) )
  19. print "quarter is still an incredible amount: " str( qrtr( debt ) )
  20. print "a king could wish for so much: " str( tenth( debt ) )
  21. 'End of Main Program Code
  22.  
  23. 'now you can define the declared function
  24. function tenth(double amount) as double
  25.         return ( amount / 10 ) 'in c, it is recommended to return within ()
  26. end function
  27.  

Charles Pegge

  • Guest
Re: Functions Example
« Reply #1 on: June 16, 2011, 06:52:34 AM »
Nice one Kent,

This is a variation with inner functions and a few other tricks!

I typedef'ed double so that "double half" and "double quarter" are avoided :)

Code: OxygenBasic
  1.  
  2. '===============
  3. 'INNER FUNCTIONS
  4. '===============
  5.  
  6. function DebtHorror(double debt,string datetime ) as string
  7.  
  8.   typedef double vt
  9.   '
  10.  vt half(vt amount)=amount*.5
  11.   vt quarter(vt amount)=amount*.25
  12.   vt tenth(vt amount)=amount*.1
  13.   '
  14.  string cr=chr(13)+chr(10)
  15.   '
  16.  return _
  17.   '
  18.  "The U.S. debt as of " datetime " = " debt cr+
  19.   "half of that is still so huge: " half( debt )  cr+
  20.   "quarter is still an incredible amount: " quarter( debt ) cr+
  21.   "a king could wish for so much: " tenth( debt )
  22.  
  23. end function
  24.  
  25. print DebtHorror 14432360075155.42, "6/16/2011  8:45 A.M."
  26.  
  27.  

Charles
« Last Edit: June 16, 2011, 06:59:32 AM by Charles Pegge »

kryton9

  • Guest
Re: Functions Example
« Reply #2 on: June 16, 2011, 07:06:40 AM »
Just curious to see if we needed the typedef and also to see if the return could be enclosed in ( ) over multiple lines, happy to say yes to both.
Code: OxygenBasic
  1. '===============  
  2. 'INNER FUNCTIONS
  3. '===============  
  4.  
  5. function DebtHorror(double debt,string datetime ) as string  
  6.  
  7.         double half( double amount ) = amount * .5  
  8.         double quarter( double amount ) = amount * .25  
  9.         double tenth( double amount ) = amount * .1  
  10.  
  11.         string cr = chr(13) + chr(10)  
  12.  
  13.         return (  "The U.S. debt as of " datetime " = " debt cr+  
  14.                         "half of that is still so huge: " half( debt )  cr+  
  15.                         "quarter is still an incredible amount: " quarter( debt ) cr+  
  16.                         "a king could wish for so much: " tenth( debt )   )
  17.    
  18. end function  
  19.  
  20. print DebtHorror 14432360075155.42, "6/16/2011  8:45 A.M."
  21.  

Aurel

  • Guest
Re: Functions Example
« Reply #3 on: June 16, 2011, 08:31:20 AM »
When we talk about functions i just test one simple but very useful function
which return number of delimited fields in given string.
Code: OxygenBasic
  1. 'function - count fields in string
  2. DECLARE FUNCTION FieldCount(String s,String d) As Int
  3.  
  4. Function FieldCount(string source,string delimiter)
  5.         Dim delpos,nexpos,count As Int
  6.         'delimiter=chr$(92)
  7.         count=1
  8.         delpos=1
  9.         nexpos=InStr(source,delimiter)
  10.         While nexpos > 0
  11.                 delpos=nexpos
  12.                 nexpos=InStr(source,delimiter,delpos+1)
  13.                 count=count+1
  14.         Wend
  15.         Return delpos-1
  16. End Function
  17.  
  18. INT fields
  19. STRING src,d$
  20. d$=" "
  21. src="This is a delimited string"
  22.  
  23. 'call function
  24. fields=FieldCount(src,d$)
  25.  
  26. Print Str(fields)
  27.  
« Last Edit: June 17, 2011, 10:57:55 AM by o2admin »

kryton9

  • Guest
Re: Functions Example
« Reply #4 on: June 16, 2011, 10:50:04 AM »
Aurel, you can edit your post and put your source code in blocks like this to get syntax highlighting.

Press the # icon in message toolbars. You will see (code)(/code), well you will see them in brackets []. I just used () so that you can see what I meant without it creating the blocks.
Paste your code between the 2 code blocks.

Now change, (code)  to (code=o2), again replace the () with []
« Last Edit: June 16, 2011, 10:52:19 AM by kryton9 »

Charles Pegge

  • Guest
Re: Functions Example
« Reply #5 on: June 16, 2011, 01:30:58 PM »

Thanks Aurel.

This extends your idea by supporting quoted fields. Delimiters inside the quotes are not counted.

Code: OxygenBasic
  1.   '---------------------------------------------
  2.  function fieldcount(string s, string d) as sys
  3.   '=============================================
  4.  sys a,b,c,le,i
  5.   '
  6.  'EXCLUDE EMPTY STRINGS
  7.  '
  8.  le=len s
  9.   if le=0 then return 0
  10.   if d="" then return 1
  11.   '
  12.  b=asc d 'delimiter ascii code
  13.  c=1     'at least 1 field
  14.  i=0     'char index
  15.  do
  16.     i++
  17.     if i>le then exit do
  18.     a=asc s,i
  19.     '
  20.    'SKIP QUOTED TEXT " ' `
  21.    '
  22.    if a=34 or a=39 or a=96 then
  23.       do
  24.         i++
  25.         if i>le then exit do
  26.         if asc(s,i)=a then  exit do
  27.       end do
  28.     end if
  29.     if a=b then c++
  30.   end do
  31.   return c
  32.   end function
  33.  
  34.  
  35.   INT fields
  36.   STRING src,d$
  37.   d$=" "
  38.   src="This is a delimited string 'and this a quoted field'"
  39.  
  40.   'call function
  41.  fields=FieldCount(src,d$)
  42.  
  43.   Print Str(fields)
  44.  
  45.  

Charles

Aurel

  • Guest
Re: Functions Example
« Reply #6 on: September 25, 2011, 01:56:20 PM »
Uf i miss this Charles example with quoted text.
And this is the function which i try to add in my parser.

Charles is there a way to replace do/end do loop with while/wend or do/until
becuse i dont have do/end do in ebasic?

Charles Pegge

  • Guest
Re: Functions Example
« Reply #7 on: September 25, 2011, 02:19:33 PM »
My fingers produce do loops automatically but while loops are slightly shorter anyway.

Code: OxygenBasic
  1. '---------------------------------------------  
  2.  function fieldcount(string s, string d) as sys  
  3.   '=============================================  
  4.  sys a,b,c,le,i  
  5.   '  
  6.  'EXCLUDE EMPTY STRINGS  
  7.  '  
  8.  le=len s  
  9.   if le=0 then return 0  
  10.   if d="" then return 1  
  11.   '  
  12.  b=asc d 'delimiter ascii code  
  13.  c=1     'at least 1 field  
  14.  i=0     'char index  
  15.  while i<le
  16.    i+=1
  17.    a=asc s,i  
  18.     '  
  19.    'SKIP QUOTED TEXT " ' `  
  20.    '  
  21.    if a=34 or a=39 or a=96 then  
  22.       while i<le  
  23.         i+=1  
  24.         if i>le then exit while  
  25.         if asc(s,i)=a then  exit while  
  26.       wend  
  27.     end if  
  28.     if a=b then c+=1  
  29.   wend  
  30.   return c  
  31.   end function  
  32.  
  33.  
  34.   INT fields  
  35.   STRING src,d$  
  36.   d$=" "  
  37.   src="This is a delimited string 'and this a quoted field'"  
  38.  
  39.   'call function  
  40.  fields=FieldCount(src,d$)  
  41.  
  42.   Print Str(fields)
  43.  
  44.  

Charles

Aurel

  • Guest
Re: Functions Example
« Reply #8 on: September 25, 2011, 09:53:28 PM »
Thank you.. ;)

Aurel

  • Guest
Re: Functions Example
« Reply #9 on: September 26, 2011, 05:28:47 AM »
Charles maby im on right track to string extractor.
So next code present current shape of extractor which i use .
My question is how i can extract arguments into string array using
number of arguments as size of array.This sounds ok for me,right?
I know that i can use normal For/next loop...
but what with qouted string ,i mean how detect in this for loop
qouted string as one argument and extract complet quoted string as one
argument even is coma character inside this quoted string?
As you see i use coma as delimiter....

Any idea if you have time for this....

this is modified code:
Code: OxygenBasic
  1. '---------------------------------------------  
  2.  function fieldcount(string s, string d) as sys  
  3.   '=============================================  
  4.  sys a,b,c,le,i  
  5.   '  
  6.  'EXCLUDE EMPTY STRINGS  
  7.  '  
  8.  le=len s  
  9.   if le=0 then return 0  
  10.   if d="" then return 1  
  11.   '  
  12.  b=asc d 'delimiter ascii code  
  13.  c=1     'at least 1 field  
  14.  i=0     'char index  
  15.  while i<le
  16.    i+=1
  17.    a=asc s,i  
  18.     '  
  19.    'SKIP QUOTED TEXT " ' `  
  20.    '  
  21.    if a=34 or a=39  
  22.       while i<le  
  23.         i+=1  
  24.         if i>le then exit while  
  25.         if asc(s,i)=a then  exit while  
  26.       wend  
  27.     end if  
  28.     if a=b then c+=1  
  29.   wend  
  30.   return c  
  31.   end function  
  32.  
  33.  
  34.   INT numArg  'number of Arguments
  35.  STRING src,d$  
  36.   d$=","  
  37.   src="arg1,arg2,arg3,arg4,'and with coma, quoted field',arg6"  
  38.  
  39.   'call function  
  40.  numArg=FieldCount(src,d$)  
  41.  
  42.   Print Str(numArg)
  43.  
  44. 'extract arguments ******************************************
  45.  
  46. INT EPos,SPos
  47. STRING arg1,arg2
  48. EPos=0
  49. SPos=0
  50. '==========================================================
  51. 'first argument
  52. IF numArg>0
  53. SPos=1
  54.  EPos = InStr(SPos,src, ",")-1
  55. Print "EPos:"+str(EPos)
  56.  If EPos <= 0 Then EPos = Len(src)
  57.  arg1 = RTrim(LTrim(Mid(src, SPos, EPos - SPos + 1)))  
  58. END IF
  59.  
  60. Print "ARG:"+arg1
  61. '=============================================================
  62. 'second argument
  63. IF numArg>1
  64.  SPos = EPos+2
  65.  EPos = InStr(SPos,src, ",") - 1
  66. Print "EPos:"+str(EPos)
  67.  If EPos <= 0 Then EPos = Len(src)
  68.  arg2 = RTrim(LTrim(Mid(src, SPos, EPos - SPos + 1)))  
  69. END IF
  70.  
  71. Print "ARG:"+arg2
« Last Edit: September 26, 2011, 05:33:25 AM by Aurel »

Charles Pegge

  • Guest
Re: Functions Example
« Reply #10 on: September 26, 2011, 08:18:29 AM »

Will this do?
All the params exclude end spaces.

Code: OxygenBasic
  1.  
  2.   '==============
  3.  'CAPTURE PARAMS
  4.  '==============
  5.  
  6.  
  7.   dim as string arg(64) 'params array
  8.  
  9.   '--------------------------------------------  
  10.  function GetFields(string s, string d) as sys    
  11.   '============================================
  12.  '
  13.  sys a,b,c,le,i
  14.   sys bg,en 'beginning and end of param    
  15.  '
  16.  '    
  17.  'EXCLUDE EMPTY STRINGS    
  18.  '    
  19.  le=len s    
  20.   if le=0 then return 0    
  21.   if d="" then return 0    
  22.   '    
  23.  b=asc d 'delimiter ascii code    
  24.  i=0     'char index    
  25.  while i<le  
  26.     i+=1  
  27.     a=asc s,i
  28.     '
  29.    'MARK START OF PARAM
  30.    '
  31.    if bg=0 then
  32.       if a<>b then
  33.         if a>32 then bg=i
  34.       end if
  35.     end if    
  36.     '    
  37.    'SKIP QUOTED TEXT " ' `    
  38.    '    
  39.    if a=34 or a=39 or a=96 then    
  40.       while i<le    
  41.         i+=1    
  42.         if i>le then exit while    
  43.         if asc(s,i)=a then  exit while    
  44.       wend    
  45.     end if
  46.     '  
  47.    'MARK END OF PARAM
  48.    '
  49.    if bg>0 then
  50.       if a<>b then
  51.         if a>32 then en=i
  52.       end if
  53.     end if
  54.     '
  55.    'DETECT DELIMITER
  56.    '
  57.    if (a=b)or(i>=le) then
  58.       c+=1 'COUNT
  59.      '
  60.      'CAPTURE PARAM
  61.      '
  62.      if bg>0 then
  63.         arg(c)=mid(s,bg,en-bg+1)
  64.       else
  65.         arg(c)=""
  66.       end if
  67.       bg=0 'CLEAR BEGIN MARKER
  68.      en=0 'CLEAR END MARKER
  69.    end if  
  70.   wend    
  71.   return c    
  72.   end function    
  73.  
  74.   sys fields  
  75.   string src,d$  
  76.   d$=","  
  77.   src="one, two,three, 'and, this, is, a, quoted, field'"  
  78.  
  79.   '
  80.  fields=getFields(src,d$)  
  81.  
  82.   Print arg(3)
  83.   Print arg(4)  
  84.  

Charles

Aurel

  • Guest
Re: Functions Example
« Reply #11 on: September 26, 2011, 10:10:54 AM »
WOW...
Excellent...Charles.
You are master of parsing that's for sure.
Thank you very much. ;)

kryton9

  • Guest
Re: Functions Example
« Reply #12 on: September 26, 2011, 05:52:24 PM »
What are you working on Aurel, got me curious?

Aurel

  • Guest
Re: Functions Example
« Reply #13 on: September 26, 2011, 10:56:53 PM »
Hi Kent

For now i want write parser in Oxygen Basic and translate some things from ABasic scripting to
Oxygen program.I also will see if i can add some parsing methods into my include files.

kryton9

  • Guest
Re: Functions Example
« Reply #14 on: September 27, 2011, 12:09:00 AM »
Thanks for filling me in. Parsing is fun, but it can get frustrating. Having routines will be nice. Good luck Aurel!