SHARED Array values with SUBs and FUNCTIONs
#5
(06-22-2023, 03:48 AM)GareBear Wrote:
(06-22-2023, 02:39 AM)Donald Foster.. Wrote: Hello All,
I usually use GOSUB for all my subroutines mainly because I'm comfortable with them and know how to use them. But there are times when I must be careful not to use the same variable names for loops when I inside a loop with the same name. For this reason, it seems SUBs and FUNCTIONs would help to prevent this problem. However, I don't know how to access ARRAYs created in the main code and SHARE it with SUBs and FUNCTIONs. I can find no examples in the WIKI.
Donald
Go to TerryRitchie's Tutorial - Chapter 6. He gives examples and explain how GOSUBS and FUNCTIONS works. I went to the wiki today to find out about FUNCTIONS and I needed more than it provided. TerryRitchie helped out on that with his Tutorial. That's what I found out today. I hope this helps you out.
The lesson in the tutorial is located here: https://www.qb64tutorial.com/lesson6

The code below shows the many ways SUBs and FUNCTIONs can be used to pass values in and out:

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
'-----------------------------------------------------------------------------------
Software and cathedrals are much the same — first we build them, then we pray.
QB64 Tutorial
Reply


Messages In This Thread
RE: SHARED Array values with SUBs and FUNCTIONs - by TerryRitchie - 06-22-2023, 01:03 PM



Users browsing this thread: 2 Guest(s)