Undefined variable

An undefined variable in the source code of a computer program is a variable that is accessed in the code but has not been declared by that code.[1]

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).

(setf y x)
*** - EVAL: variable X has no value
int main() {
  int y = x;
  return 0;
}
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.)

A ReferenceError only happens if the same piece of executed code has a let or a const (but not var) declaration later on, or if the code is executed in strict mode. In all other cases, the variable will have the special value undefined.

"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
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
Set Y=X
<UNDEF>
let y = x;;
Unbound value x
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 3

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

Python 2.4

>>> x = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined
signal on novalue
y = x
+++ Error 30 in line 2: Label not found
irb(main):001:0> y = x
NameError: undefined local variable or method `x' for main:Object
	from (irb):1
% set y $x
can't read "x": no such variable
Dim y
y = x
(no error)
Option Explicit

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

References

  1. ^ "undefined variable." YourDictionary, n.d. Web. 24 July 2013. <http://computer.yourdictionary.com/undefined-variable Archived 2013-04-15 at the Wayback Machine>.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.