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
'-----------------------
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
'-----------------------
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