HOME

TheInfoList



OR:

An undefined variable in the
source code In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
of a
computer program A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components. A computer progra ...
is a variable that is accessed in the code but has not been
declared In the sport of cricket, a declaration occurs when a captain declares his team's innings closed and a forfeiture occurs when a captain chooses to forfeit an innings without batting. Declaration and forfeiture are covered in Law 15 of the ''Laws of ...
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 program, computer programs. Most programming languages are text-based formal languages, but they may also be visual programming language, graphical. They are a kind of computer ...
, 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 computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
. 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 family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
'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 (now simply referred to as "Visual Basic"), the current version of Visual Basic launched in 2002 which runs on .NET * Visual Basic (c ...
'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 In computing, CLISP is an implementation of the programming language Common Lisp originally developed by Bruno Haible and Michael Stoll for the Atari ST. Today it supports the Unix and Microsoft Windows operating systems. CLISP includes an int ...

(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 that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...

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 Lua or LUA may refer to: Science and technology * Lua (programming language) * Latvia University of Agriculture * Last universal ancestor, in evolution Ethnicity and language * Lua people, of Laos * Lawa people, of Thailand sometimes referred t ...

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, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, D ...

let y = x;; Unbound value x


Perl Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...

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


PHP PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...
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 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 programming language that can be interpreted or compiled. It was developed at IBM by Mike Cowlishaw. It is a structured, high-level programming language designed for ease of learning and reading. ...

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


Ruby A 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 sapp ...

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 an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It allows Microsoft Windows system administrators to generate powerful tools for managing computers ...

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