Undefined Variable
   HOME

TheInfoList



OR:

An undefined variable in the
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
of a
computer program A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
is a variable that is accessed in the code but has not been declared by that code."undefined variable." YourDictionary, n.d. Web. 24 July 2013. . In some
programming languages A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their syntax (form) and semantics (meaning), usually defined by a formal language. Languages usually provide features ...
, an implicit declaration is provided the first time such a variable is encountered at
compile time In computer science, compile time (or compile-time) describes the time window during which a language's statements are converted into binary instructions for the processor to execute. The term is used as an adjective to describe concepts relat ...
. 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 Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
's "use warnings" or
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic (.NET), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (classic), the original Visual Basic suppo ...
'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

(setf y x) *** - EVAL: variable X has no value


C

int main() 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 once foo.c:2: error: for each function it appears in.)


JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...

A ReferenceError only happens if the same piece of executed code has a or a (but not ) declaration later on, or if the code is executed in strict mode. In all other cases, the variable will have the special value . "use strict"; let y = x; let y = x; let x; // causes error on line 1 ReferenceError: x is not defined Source File: file:///c:/temp/foo.js


Lua

y = x (no error, continuing) print(y) nil


ML (Standard ML of New Jersey)

val y = x; stdIn:1.9 Error: unbound variable or constructor: x


MUMPS MUMPS ("Massachusetts General Hospital Utility Multi-Programming System"), or M, is an imperative, high-level programming language with an integrated transaction processing key–value database. It was originally developed at Massachusetts Gen ...

Set Y=X


OCaml OCaml ( , formerly Objective Caml) is a General-purpose programming language, general-purpose, High-level programming language, high-level, Comparison of multi-paradigm programming languages, multi-paradigm programming language which extends the ...

let y = x;; Unbound value x


Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...

my $y = ($x // 0) + 1; # defined-or operator (no error)


PHP 5

$y = $x; (no error) $y=""; $x=""; error_reporting(E_ALL); $y = $x; PHP Notice: Undefined variable: x in foo.php on line 3


Python


Python 3

x = y Traceback (most recent call last): File "", line 1, in x = y NameError: name 'y' is not defined


Python 2.4

>>> x = y Traceback (most recent call last): File "", line 1, in NameError: name 'y' is not defined


REXX Rexx (restructured extended executor) is a high-level programming language developed at IBM by Mike Cowlishaw. Both proprietary and open-source software, open source Rexx interpreter (computing), interpreters exist for a wide range of comput ...

signal on novalue y = x +++ Error 30 in line 2: Label not found


Ruby Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...

irb(main):001:0> y = x NameError: undefined local variable or method `x' for main:Object from (irb):1


Tcl

% set y $x can't read "x": no such variable


VBScript VBScript (Microsoft Visual Basic Scripting Edition) is a deprecated programming language for scripting on Microsoft Windows using Component Object Model (COM), based on classic Visual Basic and Active Scripting. It was popular with system admi ...

Dim y y = x (no error) Option Explicit Dim y y = x (3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'


References

{{DEFAULTSORT:Undefined Variable Variable (computer science) Software bugs