Oxygen Basic

Programming => Example Code => General => Topic started by: Charles Pegge on June 16, 2011, 01:40:51 AM

Title: single line procedures
Post by: Charles Pegge on June 16, 2011, 01:40:51 AM
These functions all perform the same operation

Code: OxygenBasic
  1.  
  2. function f(sys v) as sys (return v*2)
  3.  
  4. sys f(sys v)(return v*2)
  5.  
  6. function f(sys v) as sys {return v*2}
  7.  
  8. sys f(sys v){return v*2}
  9.  
  10. sys f(sys v)=v*2
  11.  
  12. 'longer forms:
  13. '----------------
  14.  
  15. function f(byval v as sys) as sys
  16.   function=v*2
  17. end function
  18.  
  19. sys f(sys v)
  20. {
  21.   return v*2
  22. }
  23.  
  24. print f 3 'result: 6
  25.  
  26.  

Charles
Title: Re: single line procedures
Post by: kryton9 on June 16, 2011, 04:36:07 AM
This is a great way to teach oxygen without writing a manual. Thanks Charles.

Just out of curiosity, which of those functions does oxygen use to evaluate print f 3 in that example?
Title: Re: single line procedures
Post by: Charles Pegge on June 16, 2011, 06:04:33 AM

If functions have the same name and the same prototypes then the last to be defined will be used. This also applies to methods in classes and can be used to override an inherited method.

You can also put functions inside other functions and methods. Like local variables, these cannot be seen from the outside.

The only problem is that there are less constraints than in standard basic and therefore more scope for untrapped errors, but on the other  hand bringing  different pieces of code together should be much easier.

Charles


Title: Re: single line procedures
Post by: kryton9 on June 16, 2011, 06:52:11 AM
I like how you are doing it. It makes sense and makes for someone being able to override an existing method easier, in case they need to.