Author Topic: Named Parameters  (Read 4386 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Named Parameters
« on: March 26, 2011, 02:43:37 PM »

Default values may be specified in the prototype of a function. Then it is possible to pass selected variables by name. (Like an HTML tag)

Charles

Code: [Select]
  '================
  'NAMED PARAMETERS
  '================



  '--------------------------------------------------------------------------------
  function f ( a=10 as sys, sys b=20 as sys, c=30 as sys, d=40 as sys, e=50 as sys)
  '================================================================================
  '
  print "PARAM VALUES:
  A  " a "
  B  " b "
  C  " c "
  D  " d "
  E  " e "
  ."
  '
  end function


  '====
  'TEST
  '====

  f (a=1)

  f (a=1,c=3)

  f
  (
    b=2,
    e=5
  )


Charles Pegge

  • Guest
Re: Named Parameters
« Reply #1 on: March 26, 2011, 02:51:37 PM »

Default parameters are also supported with OOP methods.

Code: [Select]

  '================
  'NAMED PARAMETERS
  '================


  '-----------
  class tester
  '===========

  private
  long va,vb,vc,vd,ve

   method constructor ( a=10 as sys, sys b=20 as sys, c=30 as sys, d=40 as sys, e=50 as sys)
  '=========================================================================================
  '
  va<=a,b,c,d,e
  '
  print "PARAM VALUES:
  A  " va "
  B  " vb "
  C  " vc "
  D  " vd "
  E  " ve "
  ."
  '
  end method

  method destructor()
  '==================
  '
  end method

  end class


  '====
  'TEST
  '====

  new tester t 1,2,3,4,5
  del t
  new tester t
  del t
  new tester t()
  del t
  new tester t(a=1,e=5)
  del t

  'print structureof tester


Peter

  • Guest
Re: Named Parameters
« Reply #2 on: March 26, 2011, 04:15:10 PM »
Hi Charles,

Why cannot  we write : 1ah, 10h, ffh and so on?
Or we write always  in C style:  0xFF, 0x10 !

Charles Pegge

  • Guest
Re: Named Parameters
« Reply #3 on: March 26, 2011, 04:25:05 PM »
Sorry Peter. I forgot all about this capability. Try this! :)

Code: [Select]

print 20h

print 20o

print 10b


Charles

PS: with hexadeimal 'a..f' use a leading '0' eg: 0ffh
« Last Edit: March 26, 2011, 04:28:27 PM by Charles Pegge »

Aurel

  • Guest
Re: Named Parameters
« Reply #4 on: March 26, 2011, 10:00:40 PM »
Finaly we dont need to write always byval or byref - im not used to this
type of syntax.Now looks far better and simplier.
Thanks Charles!

Charles Pegge

  • Guest
Re: Named Parameters
« Reply #5 on: March 28, 2011, 02:20:27 AM »

Hi Aurel,

There is also achoice for setting the default mode as Byval or Byref

#byval
#byref

Code: [Select]

  #byval

  sub f(a as long) as long
  '=======================
    a=4
  end function

  a=2
  f a
  print a

  #byref

  sub f(a as long) as long
  '=======================
    a=4
  end function

  a=2
  f a
  print a

Charles

kryton9

  • Guest
default byref?
« Reply #6 on: June 20, 2012, 02:52:54 PM »
I was doing a test to check that Oxygen passes arguments ByRef by default.

Here is the test code. It won't compile, as it sees the parameters as ByVal.

Later on I specified ByRef and it works. So should I assume they are passed as ByVal?
Code: OxygenBasic
  1. 'ByRef default arguement passing test
  2. cr = chr( 10 ) + chr ( 13 )
  3.  
  4. sub testSys( sys aSys )
  5.     aSys = 42
  6. end sub
  7.  
  8. sub testDouble( double aDouble )
  9.     aDouble = 42.42
  10. end sub
  11.  
  12. sub testString( string aString ) 'so passed ByVal?
  13.    aString = "Forty-Two"
  14. end sub
  15.  
  16.  
  17. sys i = 0
  18. double d = 0.0
  19. string s = "Zero"
  20.  
  21. string txtOut = ""
  22.  
  23. txtOut += "Testing sys passed as default ByRef"+ cr
  24. txtOut += "i before sub call, should be 0 : i = " + str( i ) + cr
  25. testSys( i )
  26. txtOut += "i after sub call, should be 42 : i = " + str( i ) + cr
  27. txtOut += cr + cr
  28.  
  29. txtOut += "Testing double passed as default ByRef"+ cr
  30. txtOut += "d before sub call, should be 0.0 : d = " + str( d, 2 ) + cr
  31. testDouble( d )
  32. txtOut += "d after sub call, should be 42.42 : d = " + str( d, 2 ) + cr
  33. txtOut += cr + cr
  34.  
  35. txtOut += "Testing string passed as default ByRef"+ cr
  36. txtOut += "s before sub call, should be Zero : s = " + s + cr
  37. testString( s )
  38. txtOut += "s after sub call, should be Forty-Two : s = " + s + cr
  39. txtOut += cr + cr
  40.  
  41. print txtOut
  42.  
  43. sub test2Sys( ByRef sys aSys )
  44.     aSys = 42
  45. end sub
  46.  
  47. sub test2Double( ByRef double aDouble )
  48.     aDouble = 42.42
  49. end sub
  50.  
  51. sub test2String( ByRef string aString )
  52.     aString = "Forty-Two"
  53. end sub
  54.  
  55.  
  56. i = 0
  57. d = 0.0
  58. s = "Zero"
  59.  
  60. string txtOut = ""
  61.  
  62. txtOut += "Testing sys passed ByRef"+ cr
  63. txtOut += "i before sub call, should be 0 : i = " + str( i ) + cr
  64. test2Sys( i )
  65. txtOut += "i after sub call, should be 42 : i = " + str( i ) + cr
  66. txtOut += cr + cr
  67.  
  68. txtOut += "Testing double passed ByRef"+ cr
  69. txtOut += "d before sub call, should be 0.0 : d = " + str( d, 2 ) + cr
  70. test2Double( d )
  71. txtOut += "d after sub call, should be 42.42 : d = " + str( d, 2 ) + cr
  72. txtOut += cr + cr
  73.  
  74. txtOut += "Testing string passed ByRef"+ cr
  75. txtOut += "s before sub call, should be Zero : s = " + s + cr
  76. test2String( s )
  77. txtOut += "s after sub call, should be Forty-Two : s = " + s + cr
  78. txtOut += cr + cr
  79.  
  80. print txtOut

Charles Pegge

  • Guest
Re: Named Parameters
« Reply #7 on: June 20, 2012, 03:36:44 PM »
Hi Kent,
Values are only assumed byref with Basic syntax:

function  f(a as string,b as double)

Otherwise they are byval, unless explicity indicated:

function f(string*a, double*b)

The behaviour of the classical basic prototype, however, may be changed by the global directive:
#byval and there is also #byref to revert to the default behaviour.

Charles

kryton9

  • Guest
Re: Named Parameters
« Reply #8 on: June 20, 2012, 05:49:41 PM »
Thanks Charles, that clears up my confusion when I look at example files. I don't know how your parser keeps track of all of these options and you in turn it. Thanks again.

Charles Pegge

  • Guest
Re: Named Parameters
« Reply #9 on: June 20, 2012, 07:44:24 PM »
Well, I'm keeping it a small language. Every keyword has to justify itself :)