Wikipedia

Undefined variable

Also found in: Dictionary, Medical, Financial, Encyclopedia.

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

CLISP

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

C

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

JavaScript

y = x 
 Error: 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

Set Y=X 
<UNDEF> 

OCaml

let y = x;; 
Unbound value x 

Perl

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 2.4

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

REXX

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

Ruby

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

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>.
This article is copied from an article on Wikipedia® - the free encyclopedia created and edited by its online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of Wikipedia® encyclopedia articles provide accurate and timely information, please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.

Copyright © 2003-2025 Farlex, Inc Disclaimer
All content on this website, including dictionary, thesaurus, literature, geography, and other reference data is for informational purposes only. This information should not be considered complete, up to date, and is not intended to be used in place of a visit, consultation, or advice of a legal, medical, or any other professional.