An undefined variable in the source code of a computer program is a variable that is accessed in the code but has not been previously declared by that code.
Contents
In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time. In other languages such a usage is considered to be sufficiently serious that a diagnostic being issued and the compilation fails.
Some language definitions initially used the implicit declaration behavior and as they matured provided an option to disable it (e.g. Perl's "use warnings
" or Visual Basic's "Option Explicit
").
Examples
The following provides some examples of how various programming language implementations respond to undefined variables. Each code snippet is followed by an error message (if any).
CLISP
*** - EVAL: variable X has no valueC
foo.c: In function `main':foo.c:2: error: `x' undeclared (first use in this function)foo.c:2: error: (Each undeclared identifier is reported only oncefoo.c:2: error: for each function it appears in.)JavaScript
Error: x is not defined Source File: file:///c:/temp/foo.jsLua
(no error, continuing)
nil