06-22-2023, 03:37 PM
When I use the IDE to Export As... Forum codebox (to clipboard) I get prettier code (see below), but still the unaligned text.
Code: (Select All)
TYPE StarType ' definition of a star
x AS INTEGER ' x coordinate
y AS INTEGER ' y coordinate
END TYPE
DIM Star(100) AS StarType ' star array (LOCAL to the main program level)
DIM s AS INTEGER ' generic counter (LOCAL to the main program level)
DIM ScreenWidth AS INTEGER ' width of screen (LOCAL to the main program level)
DIM ScreenHeight AS INTEGER ' height of screen (LOCAL to the main program level)
RANDOMIZE TIMER ' seed random number generator
ScreenWidth = 640 ' set screen width
ScreenHeight = 480 ' set screen height
'-----------------------------------------------------------------------------------
' A subroutine using GOSUB and a local array
GOSUB PopulateStars
PRINT UBOUND(Star); "stars created (array LOCAL at main program level using GOSUB)"
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' A subroutine using SUB and an array passed by reference
Populate_Stars Star()
PRINT UBOUND(Star); "stars created (array passed by reference into SUB)"
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' A subroutine using SUB and an array that has been SHARED
Populate__Stars
PRINT UBOUND(Star); "stars created (array SHARED in SUB)"
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' A function using FUNCTION and an array passed by reference
PRINT Populate_and_count_Stars(Star()); "stars created (array passed by reference into FUNCTION)"
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' A function using FUNCTION and an array that has been SHARED
PRINT Populate_and_count_Stars2; "stars created (array SHARED in FUNCTION)"
'-----------------------------------------------------------------------------------
END
'-----------------------------------------------------------------------------------
' Everything LOCAL to the main program level
'-----------------------------------------------------------------------------------
PopulateStars: ' subroutine to populate Star() array
FOR s = 1 TO UBOUND(Star) ' cycle through array
Star(s).x = INT(RND * ScreenWidth) ' create random star coordinates
Star(s).y = INT(RND * ScreenHeight)
NEXT s
RETURN ' return to next command statement
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' An array passed into a subroutine by reference
'-----------------------------------------------------------------------------------
SUB Populate_Stars (Array() AS StarType)
' Any changes in Array() will be passed back to Star()
' Star() has been passed 'by reference' into Array()
SHARED ScreenWidth AS INTEGER ' share the variable from main program level
SHARED ScreenHeight AS INTEGER ' share the variable from main program level
DIM s AS INTEGER ' generic counter (LOCAL to this subroutine)
FOR s = 1 TO UBOUND(Array) ' cycle through array
Array(s).x = INT(RND * ScreenWidth) ' create random star coordinates
Array(s).y = INT(RND * ScreenHeight)
NEXT s
END SUB ' return to next command statement
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' A SHARED array from the main program level
'-----------------------------------------------------------------------------------
SUB Populate__Stars ()
' Any changes in Star() will be saved when subroutine exits
SHARED ScreenWidth AS INTEGER ' share the variable from main program level
SHARED ScreenHeight AS INTEGER ' share the variable from main program level
SHARED Star() AS StarType ' share the array from the main program level
DIM s AS INTEGER ' generic counter (LOCAL to this subroutine)
FOR s = 1 TO UBOUND(Star) ' cycle through array
Star(s).x = INT(RND * ScreenWidth) ' create random star coordinates
Star(s).y = INT(RND * ScreenHeight)
NEXT s
END SUB ' return to the next command statement
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' An array passed into a function by reference
'-----------------------------------------------------------------------------------
FUNCTION Populate_and_count_Stars (Array() AS StarType)
' Any changes in Array() will be passed back to Star()
' Star() has been passed 'by reference' into Array()
SHARED ScreenWidth AS INTEGER ' share the variable from main program level
SHARED ScreenHeight AS INTEGER ' share the variable from main program level
DIM s AS INTEGER ' generic counter (LOCAL to this subroutine)
FOR s = 1 TO UBOUND(Array) ' cycle through array
Array(s).x = INT(RND * ScreenWidth) ' create random star coordinates
Array(s).y = INT(RND * ScreenHeight)
NEXT s
Populate_and_count_Stars = UBOUND(Array) ' return size of the array passed in
END FUNCTION ' return to the next command statement
'-----------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------
' A SHARED array from the main program level
'-----------------------------------------------------------------------------------
FUNCTION Populate_and_count_Stars2 ()
' Any changes in Star() will be saved when function exits
SHARED ScreenWidth AS INTEGER ' share the variable from main program level
SHARED ScreenHeight AS INTEGER ' share the variable from main program level
SHARED Star() AS StarType ' share the array from the main program level
DIM s AS INTEGER ' generic counter (LOCAL to this subroutine)
FOR s = 1 TO UBOUND(Star) ' cycle through array
Star(s).x = INT(RND * ScreenWidth) ' create random star coordinates
Star(s).y = INT(RND * ScreenHeight)
NEXT s
Populate_and_count_Stars2 = UBOUND(Star) ' return size of the array passed in
END FUNCTION ' return to the next command statement
'-----------------------------------------------------------------------------------