Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
DAY 022: CLEAR
#1
Do you have a clear idea of what the KEYWORD CLEAR does? If not, clear your
schedule while I clear up what CLEAR does in a clear and concise manner. So let's
clear the way and get started with some helpful do's and don't's (Hey, a double apostrophe siting!)
when using CLEAR; clear enough? If not, clear out now...

CLEAR zeros every numeric variable and nullifies every string.

CLEAR zeros the value of all arrays and also removes (de-initializes) the dimension range. All arrays will need to be re-diminsioned.

CLEAR zeros and nullifies all STATIC variables. (Variables that would otherwise retain their values in a sub-routine).

CLEAR cannot be used in a sub routine or function, only in the main.

CLEAR does not affect settings like VIEW PRINT, colors with PALETTE of fonts, window size, or cursor position, etc.

CLEAR does not destroy TYPE structure. It just makes all TYPE variables zero and all TYPE strings null.

For example...

Code: (Select All)
CONST pi = 3.14
PALETTE 0, 8 ' Black background to dark blue.
PALETTE 8, 63 ' Gray to bright white text.
DIM pete AS Perfect
TYPE Perfect
    s AS STRING
    v AS INTEGER
END TYPE

pete.s = "Pete"
COLOR 8, 0

101
PRINT pete.s;
SLEEP
CLEAR: pete.s = " is better than Steve and pi is " + LTRIM$(STR$(pi))
GOTO 101

CLEAR in QuickBasic had 2 parameters that are ignored in QB64. They were used to clear up specified amounts of stack space and memory.

CLEAR does not remove any PCOPY info.

CLEAR does not close any open files.

CLEAR acts a bit different than RUN in certain situations...

Code: (Select All)
' Let's locate and print off-screen and try to redo it. This works with CLEAR but not with RUN.
101
LOCATE 30, 1
PRINT "PRINT "Press a key loop again."
SLEEP
CLEAR
GOTO 101

Try doing that with RUN and it errors out at the LOCATE statement.

Code: (Select All)
' Let's locate and print off-screen and try to redo it. This works with CLEAR but not with RUN.
LOCATE 30, 1
PRINT "PRINT "Press a key loop again."
SLEEP
RUN

So in the first run QB64 acts like QBasic, it adjusts the screen to accommodate the off-screen print location.With CLEAR, it works but with RUN it does not duplicate that initial accommodation.

So here is the most interesting event I've found working with CLEAR over the years, the need to re-initialize a STATIC array.

Code: (Select All)
101
testsub

PRINT: PRINT "Okay, press a key..."
SLEEP
CLS
_DELAY .5
CLEAR
REDIM SHARED testarray(20) '<----------- We must remember to reinitialize our array.
GOTO 101

SUB testsub
    STATIC testarray(20)
    FOR i = 1 TO 20
        testarray(i) = i
        PRINT testarray(i): _DELAY .05
    NEXT
END SUB

For fun, try that again with the REDIM line REMmed out. It will error out at line 15.

What I think the developers should look at is RUN. RUN should not require we reinitialize anything.

This error out...
Code: (Select All)
testsub

PRINT: PRINT "Okay, press a key..."
SLEEP
CLS
_DELAY .5
RUN

SUB testsub
    STATIC testarray(20)
    FOR i = 1 TO 20
        testarray(i) = i
        PRINT testarray(i): _DELAY .05
    NEXT
END SUB

Now to get that to work would require we once again initialize our array, but this time at the top of the program.

REDIM SHARED testarray(20) ' <----------- Place this at the top of the code above and it will work.

Other differences between CLEAR and RUN are...

RUN can have a line number like RUN 101. CLEAR would need to be written as: CLEAR: GOTO 101

RUN sets the cursor to the top left. CLEAR maintains the current position.

RUN resets the RANDOMIZE sequence to the starting RND function value. (From the wiki). Clear does not reset the RND pattern.

RUN does not preserve the PALETTE or COLOR, but CLEAR does.

RUN does not preserve VIEW PRINT, but CLEAR does.

RUN and CLEAR both preserve PCOPY memory.

In general it is better practice to keep a list of all your variables and "clear" the ones you need cleared, but CLEAR
is a quick way of obtaining the same results when all your variables need to be cleared.

Another advantage of CLEAR over RUN is you can insert program control flow variables after the CLEAR statement.

Code: (Select All)
PRINT "What is my route? ": PRINT
101
SELECT CASE myroute
    CASE 0
        PRINT "Route 1"
    CASE 1
        PRINT "Route 2"
END SELECT

SLEEP
CLEAR: myroute = 1
GOTO 101

Finally RUN can be used in a SUB or FUNCTION, but CLEAR cannot. This is considered bad practice, because it creates a stack leak, which in time with excessive use will cause your program to crash.


Pete
Reply


Messages In This Thread
DAY 022: CLEAR - by Pete - 12-03-2022, 12:47 AM
RE: DAY 022: CLEAR - by mnrvovrfc - 12-03-2022, 02:44 AM
RE: DAY 022: CLEAR - by Pete - 12-03-2022, 02:52 AM
RE: DAY 022: CLEAR - by bplus - 12-03-2022, 04:50 AM



Users browsing this thread: 2 Guest(s)