|
Subroutines
Using subroutines in BT Basic is pretty intuitive with the exception the the scope of variables. Listing 1 demostrates two
different methods of creating a subroutine variable that is global to both the main program and other subroutines/functions
and the one method that defines a variable as local. The variable considered in this program is "T". The main program
initializes "T" to a value of 5, but the call to sub One, which declares "T" to be global changes its
value to 3. The subsequent call to sub Two, where "T" is local only, changes the value of the local variable, but
does not modify the value of the global version. The call to sub Three passes "T" to the subroutine. While sub Three
does not explicitly declare "T" global, the passing of the variable implicitly makes it global.
Listing 2 shows the output of the program in Listing 1. Note that the program output is designed to show the flow and
the effects of the various calls on variable "T".
|
Listing #1
T = 5
print " In the Main program T is intialized to "&val$(T)
call One
print " On return to the Main program from sub One T = "&val$(T)
call Two
print " On return to the Main program from sub Two T = "&val$(T)
call One
print " On return to the Main program from sub One T = "&val$(T)
call Three(T)
print " On return to the Main program from sub Three T = "&val$(T)
end
sub One
global T
print" "
print " sub One"
print " Declare T global"
print " On entry T = "&val$(T)
T = 3
print " Set T to 3"
print " On exit T = "&val$(T)
print " subend"
print" "
subend
sub Two
print " "
print " sub Two"
print " T is local only"
print " On entry to sub Two T = "&val$(T)
T = 7
print " Set T to 7 in sub Two"
print " On exit from sub Two T = "&val$(T)
print " subend"
print" "
subend
sub Three(T)
print " "
print " sub Three"
print " T is a parameter passed in the call to sub Three"
print " On entry to sub Three T = "&val$(T)
T = 9
print " Set T to 9 in sub Three"
print " On exit from sub Three T = "&val$(T)
print " subend"
print " "
subend
|
|
Listing #2
In the Main program T is intialized to 5
sub One
Declare T global
On entry T = 5
Set T to 3
On exit T = 3
subend
On return to the Main program from sub One T = 3
sub Two
T is local only
On entry to sub Two T = 0
Set T to 7 in sub Two
On exit from sub Two T = 7
subend
On return to the Main program from sub Two T = 3
sub One
Declare T global
On entry T = 3
Set T to 3
On exit T = 3
subend
On return to the Main program from sub One T = 3
sub Three
T is a parameter passed in the call to sub Three
On entry to sub Three T = 3
Set T to 9 in sub Three
On exit from sub Three T = 9
subend
On return to the Main program from sub Three T = 9
|
Functions
Listing 3 is an example of declaring and accessing a function. Functions operate in much the same way as subroutines, except
they return a value. A function may return any valid variable type. The definition of the function determines the variable
type to be returned. If the function in Listing 3 was declared as "def fnCelsius$", the return value must be a string
variable, rather than an integer variable. Variables may be declared global in a function in the same manner as subroutines.
This program also demonstrates the BT Basic "input" command and demonstrates the usage of output imaging.
|
Listing #3
D80:image ddd.dd,25a,ddd.dd,16a
loop
input"Enter temperature in Farenheit ",Z
print using"@"
K = Z
Z = fnCelsius(Z)
print using D80;K," Degrees Farenheit equals ",Z," Degress Celsius"
print " "
input"Hit any key to continue",A$
print using"@"
exit if K = 212
end loop
end
def fnCelsius(T)
T = T - 32
T = T / 1.8
return(T)
fnend
|
|