Author Topic: optional parameter does not kick in  (Read 3246 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
optional parameter does not kick in
« on: July 10, 2011, 12:06:04 PM »
I have noticed before, but many times by enclosing in () solved it. But here is an example where it doesn't kick in Charles.
Code: OxygenBasic
  1. 'optional parameters behaviour
  2.  
  3. function power( sys number, optional sys exponent = 2 )
  4.     return pow( number, exponent )
  5. end function
  6.  
  7. print str( power 2 ) 'the optional parameter does not kick in
  8. print str( pow( 2, 4) )
  9. print str( power 2, 4 )
  10. print str( 2^4 )

Peter

  • Guest
Re: optional parameter does not kick in
« Reply #1 on: July 10, 2011, 01:29:26 PM »
Hi Kent,

There is no OPTIONAL!

Charles Pegge

  • Guest
Re: optional parameter does not kick in
« Reply #2 on: July 10, 2011, 02:40:24 PM »
Optional parameters and named parameters are two different paradigms

These use named params

Code: OxygenBasic
  1. 'named parameters behaviour  
  2.  
  3. function power( sys number=2, sys exponent = 2 ) as double
  4.     return pow( number, exponent )  
  5. end function  
  6.  
  7. print  str power ()
  8. print  str power (4, 2)
  9. print  str power (exponent=5)
  10.  

and these use optional params (which always default to null)

Code: OxygenBasic
  1. 'optional parameters behaviour  
  2.  
  3. function power( optional sys number, exponent ) as double
  4.     if  number=0 then number=1
  5.     if exponent=0 then exponent=1
  6.     return pow( number, exponent )  
  7. end function  
  8.  
  9. print  str power ()
  10. print  str power (4)
  11. print  str power (4,3)
  12.  
  13.  


Charles
« Last Edit: July 10, 2011, 02:48:38 PM by Charles Pegge »

Charles Pegge

  • Guest
Re: optional parameter does not kick in
« Reply #3 on: July 10, 2011, 03:46:34 PM »

Another Oxygen.dll fix. print was giving chinese characters instead of numbers under certain conditions. No need for str ..

Code: OxygenBasic
  1.  
  2. 'optional parameters behaviour    
  3.    
  4. function power( optional sys number, exponent ) as double  
  5.     if  number=0 then number=1  
  6.     if exponent=0 then exponent=1  
  7.     return pow( number, exponent )    
  8. end function    
  9.    
  10. print  power ()  
  11. print  power (4)  
  12. print  power (4,3)  
  13.  

Charles

[attachment deleted by admin]

kryton9

  • Guest
Re: optional parameter does not kick in
« Reply #4 on: July 10, 2011, 06:10:34 PM »
That is very helpful Charles. Thanks for the nice explanation. I like using optional parameters a lot, now it makes sense that it would check against NULL or 0 then assign what it is needed.

Charles Pegge

  • Guest
Re: optional parameter does not kick in
« Reply #5 on: July 10, 2011, 09:46:32 PM »
Here is another example Kent. Using optional parameters passed by reference. In these cases you need to check each address for null.

if  @number=0 then n=2 else n=number

Code: OxygenBasic
  1.  
  2. 'optional parameters behaviour
  3.  
  4. 'USING OPTIONAL PARAMETERS BY REFERENCE    
  5.    
  6. function power( optional sys *number, *exponent ) as double
  7.     sys n,e
  8.     if  @number=0 then n=2 else n=number  
  9.     if @exponent=0 then e=2 else e=exponent
  10.     return pow( n, e )    
  11. end function    
  12.    
  13. print  power ()  
  14. print  power (4)  
  15. print  power (4,3)  
  16.  

Charles
« Last Edit: July 10, 2011, 09:48:41 PM by Charles Pegge »

kryton9

  • Guest
Re: optional parameter does not kick in
« Reply #6 on: July 10, 2011, 10:26:40 PM »
Thanks Charles. Very handy information. As I go back through my class files I will update all of them as I have ConsoleC.inc to work correctly with optional parameters.