' *** SHARE_EX.BAS - SHARED statement programming example ***
' This program calls a subprogram, Convert, to convert an
' input decimal number to its string representation
' in a specified base. The program uses SHARED to allow the
' string to be shared by the subprogram and main program.
'
DECLARE SUB Convert (D%, Nb%)
DEFINT A-Z
CLS    ' Clear screen
PRINT "Enter zero or a negative number to quit": PRINT
DO
   INPUT "Decimal number: ", Decimal
   IF Decimal <= 0 THEN EXIT DO
   INPUT "New base: ", Newbase
   N$ = ""
   PRINT Decimal; "base 10 equals ";
   DO WHILE Decimal > 0
      CALL Convert(Decimal, Newbase)
      Decimal = Decimal \ Newbase
   LOOP
   PRINT N$; " base"; Newbase
   PRINT
LOOP

SUB Convert (D, Nb) STATIC
SHARED N$
   ' Take the remainder to find the value of the current
   ' digit.
   R = D MOD Nb
   ' If the digit is less than ten, return a digit (0...9).
   ' Otherwise, return a letter (A...Z).
   IF R < 10 THEN Digit$ = CHR$(R + 48) ELSE Digit$ = CHR$(R + 55)
   N$ = RIGHT$(Digit$, 1) + N$
END SUB

