Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

CLR

Keyword Abbreviation Token (hex) Version(s) Classification
CLR C{Shift+L} 9C 1.0+ Command, Preposition*, and Statement
*see GRAPHIC for use as a preposition

 
  Syntax  
CLR
 
  Purpose  
State managment.  Deletes all user variables and functions.

 
  Remarks  
CLR obstensibly deletes all user variables and DEF functions (i.e., user-created BASIC functions).  However, CLR also has numerous undocumented (well at least poorly documented) side effects which include:
  • "Forgets" all OPEN files.  This is potentially dangerous... any file opened for writing should be properly closed! 
  • Clears reserved variables DS and DS$
  • Disables error TRAPs
  • Clears reserved variable EL (error line)
  • Clears reserved variable ER (error number)
  • Clears user variables and functions (see below)
  • Sets the default USING virtual characters
  • Sets the DATA pointer to the start the program
  • Clears the BASIC stack (removes all DO/FOR/GOSUB entries)
  • Clears the CONT pointer
It is important to note that the actions listed above are not only executed when a CLR command/statement is executed, but also in the following cases:
  • (D)LOAD "fileName" (in direct mode, but not in RUN mode)
  • NEW
  • RUN (the program in memory)
  • RUN "fileName"
And that is not all!  Most of the CBM 8-bit computers only had 64K or less RAM.  So programs and variables were stored together in the same "bank" of RAM.  Thus whenever a program is modified, the variables would get "clobbered" if the program got bigger.  Now BASIC could move all the variables everytime you made a change, but it would probably make the system very slow to edit programs.  So instead these machines will perform CLR whenever you make any changes to the program, regardless if it would require moving memory or not.  The specific edit actions that will automatically perform CLR on these machines are:
  • Position the cursor over an existing program line and press RETURN (CLR is performed whether or not you made any changes to the line)
  • Enter a new program line
  • Enter a number between 0 and 63999 on a blank line and press RETURN (this deletes a line if it exists, but CLR is performed no matter what)
  • Execute DELETE
Most (all?) of the CBM-II series have 128K or more of RAM, and those that do have more than 64K store the program in a seperate BANK so editing a program can't possibly "clobber" any variables.  Unfortunately, they too will erase all your variables when you make any change to your program (or perform an act that BASIC thinks will change the program). 
 
Only the C128 allows you to freely edit your programs without destroying your variables.  This is fantastic for program developement, however there is a bug associated with this feature.  Any user functions may potentially point to the wrong address resulting in a bad definition after the change.  It depends if the program line containing a function DEF is moved in memory.  Unfortunately, the C128 will still perform CLR when you (D)LOAD a file in direct mode.  The problem with corrupt user functions is not unique to the C128 however.  Using GRAPHIC (available on other machines) to (de)allocate a bitmap will also move program memory and corrupt user functions (but thankfully will not perform CLR).
 
Please remember when you "edit" a program on any of the CBM machines (except the C128) that not only are the variables cleared (bad enough), but all the other side-effects of CLR listed above are performed as well.  In particular, it "forgets" all open files which will effectively corrupt any that were open for writing.
 
The most important part (clearing user variables and functions) is accomplished by updating several Secret Variables.  Scalar (non-array) variables (and user functions) are erased by setting the Start of Arrays equal to the Start of Variables (on machines with 64K RAM or less, this is the same as End of BASIC Program).  Arrays are erased by setting Free Space equal to Start of Variables.  Strings are erased by setting End of Strings equal to Start of Strings (on machines with 64K RAM or less, the Start of Strings is the same as End of BASIC).  Slightly off-topic: the End of Strings varies (it decreases), while the Start of Strings (or on some machines End of BASIC) is fixed.
 
Anyway, the result of the above is that all scalars, arrays, strings, and (user) functions are "errased" (technically they are still in RAM, but they have been "forgotten").
 
The following reserved variables are not affected:
  • ST
  • TI
  • TI$
Attempting to supply a parameter(s) to CLR will generate SYNTAX ERROR.
 
Example:
A = 1 : PRINT A
 1

READY.
CLR : PRINT A 
 0

READY.
 
  Compare With  
 
  Contrast With  
 
  See Also  

© H2Obsession, 2014
Comments