Undefined values: Difference between revisions

Added BBC BASIC
(Added BBC BASIC)
Line 86:
=={{header|BASIC}}==
Classic BASIC does have the concept of un-initialised or undefined variables.
 
=={{header|BBC BASIC}}==
A scalar variable (numeric or string) cannot have an 'undefined' value; if an attempt is made to read a variable which has never been defined a 'No such variable' error results. By trapping errors this condition can be detected:
<lang bbcbasic> ok% = TRUE
ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE
IF ok% THEN
PRINT variable$
ELSE
PRINT "Not defined"
ENDIF
RESTORE ERROR</lang>
 
{{works with|BBC BASIC for Windows}}
Arrays and structures however '''can''' have an undefined state; for example after having been declared as LOCAL or PRIVATE but before being defined using DIM. This condition can be detected and manipulated:
<lang bbcbasic> PROCtest
END
DEF PROCtest
LOCAL array()
IF !^array() < 2 PRINT "Array is undefined"
DIM array(1,2)
IF !^array() > 1 PRINT "Array is defined"
!^array() = 0 : REM Set array to undefined state
ENDPROC</lang>
 
=={{header|C}}==