Option _Explicit Keyword(s) of day XXX:
#22
I'm not really sure what the point of the example code was. A simple modification is all that is needed:

Code: (Select All)
OPTION _EXPLICIT

' *** = added

' It's important to note that x1, x2, and x3, declared locally in the main module are completely
' different variables than x1, x2, and x3 that are declared locally in Subx2 and Subx3.

' If the intent was to have x1, x2, and x3 seen globally then x1, x2, and x3 could be DIM SHARED
' at the main module level removing the need to declare any variables within the subroutines.

DIM SHARED x '                         declared, global    (seen everywhere)
DIM x1 '                               declared, local     (to main module only)
DIM x2 '                           *** declared, local     (to main module only)
DIM x3 '                           *** declared, local     (to main module only)

'Main Module
x = 1 '                               declared, global    (seen everywhere)
x1 = 100 '                             declared, local     (to main module only)
PRINT "Main Module values"
PRINT "Value of x  = "; x '           declared, global    (seen everywhere)
PRINT "Value of x1 = "; x1 '           declared, local     (to main module only)
PRINT "Value of x2 = "; x2 '           not declared, local (to main module only) no longer "out of the blue"
PRINT "Value of x3 = "; x3 '           not declared, local (to main module only) no longer "out of the blue"
PRINT

Subx2
Subx3

SUB Subx2
    DIM x1 '                       *** declared, local     (to subroutine only)
    DIM x2 '                           declared, local     (to subroutine only)
    DIM x3 '                       *** declared, local     (to subroutine only)
    x2 = 200 '                         declared, local     (to subroutine only)
    PRINT
    PRINT "Subx2 Module values"
    PRINT "Value of x  = "; x '       declared, global    (seen everywhere)
    PRINT "Value of x1 = "; x1 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x2 = "; x2 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x3 = "; x3 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT
END SUB

SUB Subx3
    DIM x1 '                       *** declared, local     (to subroutine only)
    DIM x2 '                       *** declared, local     (to subroutine only)
    DIM x3 '                           declared, local     (to subroutine only)
    x3 = 300 '                         declared, local     (to subroutine only)
    PRINT
    PRINT "Subx3 Module values"
    PRINT "Value of x  = "; x '       declared, global    (seen everywhere)
    PRINT "Value of x1 = "; x1 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x2 = "; x2 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x3 = "; x3 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT
END SUB

Or, this could be done to limit the amount of DIM statements if the meaning was to share values:

Code: (Select All)
OPTION _EXPLICIT

' *** = added

' It's important to note that x1, x2, and x3, declared locally in the main module are completely
' different variables than x1, x2, and x3 that are declared locally in Subx2 and Subx3.

' If the intent was to have x1, x2, and x3 seen globally then x1, x2, and x3 could be DIM SHARED
' at the main module level removing the need to declare any variables within the subroutines.

DIM SHARED x '                        declared, global    (seen everywhere)
DIM x1 '                               declared, local     (to main module only)
DIM x2 '                           *** declared, local     (to main module only)
DIM x3 '                           *** declared, local     (to main module only)

'Main Module
x = 1 '                               declared, global    (seen everywhere)
x1 = 100 '                             declared, local     (to main module only)
PRINT "Main Module values"
PRINT "Value of x  = "; x '           declared, global    (seen everywhere)
PRINT "Value of x1 = "; x1 '           declared, local     (to main module only)
PRINT "Value of x2 = "; x2 '           not declared, local (to main module only) no longer "out of the blue"
PRINT "Value of x3 = "; x3 '           not declared, local (to main module only) no longer "out of the blue"
PRINT

Subx2 x2 ' x2 passed in by reference
Subx3 x3 ' x3 passed in by reference

PRINT
PRINT "Press a key"
SLEEP
CLS

PRINT "Main Module values after calls to Subx2 and Subx3"
PRINT "Value of x  = "; x '           declared, global    (seen everywhere)
PRINT "Value of x1 = "; x1 '           declared, local     (to main module only)
PRINT "Value of x2 = "; x2 '           not declared, local (to main module only) no longer "out of the blue"
PRINT "Value of x3 = "; x3 '           not declared, local (to main module only) no longer "out of the blue"
PRINT

Subx2 x2 ' x2 passed in by reference
Subx3 x3 ' x3 passed in by reference

SUB Subx2 (x2) ' x2 is passed in by local reference, x2 here is a completely different variable from x2 at main module level
    DIM x1 '                       *** declared, local     (to subroutine only)
    DIM x3 '                       *** declared, local     (to subroutine only)
    x2 = 200 '                         declared, local     (to subroutine only) change affects x2 at main module level
    PRINT
    PRINT "Subx2 Module values"
    PRINT "Value of x  = "; x '       declared, global    (seen everywhere)
    PRINT "Value of x1 = "; x1 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x2 = "; x2 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x3 = "; x3 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT
END SUB

SUB Subx3 (x3) ' x3 is passed in by local reference, x3 here is a completely different variable from x3 at main module level
    DIM x1 '                       *** declared, local     (to subroutine only)
    DIM x2 '                       *** declared, local     (to subroutine only)
    x3 = 300 '                         declared, local     (to subroutine only) change affects x3 at main module level
    PRINT
    PRINT "Subx3 Module values"
    PRINT "Value of x  = "; x '       declared, global    (seen everywhere)
    PRINT "Value of x1 = "; x1 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x2 = "; x2 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT "Value of x3 = "; x3 '       declared, local     (to subroutine only) no longer "out of the blue"
    PRINT
END SUB
Software and cathedrals are much the same — first we build them, then we pray.
QB64 Tutorial
Reply


Messages In This Thread
RE: Option _Explicit Keyword(s) of day XXX: - by TerryRitchie - 06-09-2023, 04:51 PM



Users browsing this thread: 20 Guest(s)