Author Topic: Subroutines in Oxygen  (Read 3046 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Subroutines in Oxygen
« on: March 23, 2011, 03:30:20 AM »
In many situations, it is useful to be able to deploy a subroutine instead of a function.
Subroutines have access to all parental variables. Parameters are not used so gosubs and subroutines are extremely fast - there is no setup baggage.

The disadvantage of subroutines is lack of encapsulation. A traditional subroutine has no private variables of its own. But in Oxygen this can be rectified by adding a scope block then declaring its own private variables inside the block.

These examples show a subroutine being used inside a function.

Simple subroutine
Code: [Select]

  '-----------------------
  function fun() as string
  '=======================

  dim as long a=42, b=42

  gosub routine1 'or call routine1
  return "a=" a ",  b=" b

  exit function

  routine1:
  '--------
  '
  a=7
  b=7
  ret 'USE 'RET' NOT RETURN (interpreted as a C style return)
  '
  end function

  '====
  'MAIN
  '====

  print fun() 'RESULT: a=7, b=7

subroutine with scope block
Code: [Select]

  '-----------------------
  function fun() as string
  '=======================

  dim as long a=42, b=42

  gosub routine1 'or call routine1
  return "a=" a ",  b=" b

  exit function

  routine1:
  '--------
  '
  scope 'ALLOWS SUBROUTINE TO CREATE ITS OWN PRIVATE VARIABLES
  (
    dim as long a 'THIS CANNOT BE ACCESSED BY THE MAIN FUNCTION
    a=7 'SIMILAR  A COCAL VARIABLE
    b=7 '
  )
  ret 'USE 'RET' NOT RETURN (interpreted as a C style return)
  '
  end function

  '====
  'MAIN
  '====

  print fun() 'RESULT: a=42, b=7


Charles
« Last Edit: March 23, 2011, 12:23:01 PM by Charles Pegge »

kryton9

  • Guest
Re: Subroutines in Oxygen
« Reply #1 on: March 23, 2011, 12:01:56 PM »
Charles, that is really nice explanation. I had just woken up when reading this and it took me awhile to realize the first example Simple subroutine was being called from within a function.

I would take it out of the function and just show it as a simple subroutine or change the name to Simple subroutine from within a function.

Charles Pegge

  • Guest
Re: Subroutines in Oxygen
« Reply #2 on: March 23, 2011, 12:30:43 PM »
Hi Kent,

Thanks. I've added a sentence to that effect. Normally subroutines should only be used inside functions or subs. Otherwise you must provide a jmp or goto to skip over them to reach the program's proper termination code.

Charles

JRS

  • Guest
Re: Subroutines in Oxygen
« Reply #3 on: March 23, 2011, 02:01:07 PM »
Code: [Select]
SUB Test
a = 1
GOSUB GSRoutine
PRINT a,"\n"
END SUB

Test
END

GSRoutine:
a += 1
RETURN

jrs@Laptop:~/SB/test$ scriba gsfunc.sb
(0): error &H66:A label "main::gsroutine'test" was not defined during syntax analysis.
jrs@Laptop:~/SB/test$


Looks like ScriptBasic requires GOSUB routines to be within the SUB/FUNCTION to work. On the other hand, using the the same label name in SUB/FUNCTION's and your main routine code is allowed. Scope is well defined in SB and keeps me from shooting myself in the foot.

Charles Pegge

  • Guest
Re: Subroutines in Oxygen
« Reply #4 on: March 23, 2011, 03:00:17 PM »
Yes John, that is understandable. Script Basic has more safety ralls than O2.


Actually the Oxygen gosub is no more than an Assembler call to a label. Functions and subs by contrast require substantial prolog and epilog code to give them their encapsulation properties.

If anyone wants to use subroutines on the outside then the cleanest way to do it is to provide jump overs around each routine so you can't fall into them by accident even when they are shuffled to different positions during code development.

Code: [Select]

long a
gosub RoutineA : print a
gosub RoutineB : print a

'==================

jmp fwd over
routineA:
  a=7
ret
over:

jmp fwd over
routineB:
  a=14
ret
over:


Charles