Undefined variable
Encyclopedia
An undefined variable in the source code
Source code
In computer science, source code is text written using the format and syntax of the programming language that it is being written in. Such a language is specially designed to facilitate the work of computer programmers, who specify the actions to be performed by a computer mostly by writing source...

 of a computer program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 is a variable
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

 that is accessed in the code but has not been previously declared
Declaration (computer science)
In programming languages, a declaration specifies the identifier, type, and other aspects of language elements such as variables and functions. It is used to announce the existence of the element to the compiler; this is important in many strongly-typed languages that require variables and their...

 by that code.

In some programming languages an implicit declaration is provided the first time such a variable is encountered at compile time
Compile time
In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time.The operations performed at...

. 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
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

's "use strict" or Visual Basic
Visual Basic
Visual Basic is the third-generation event-driven programming language and integrated development environment from Microsoft for its COM programming model...

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

 (GNU CLISP 2.35):

(setf y x)


*** - EVAL: variable X has no value


C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 (GNU GCC 3.4):

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
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 (Mozilla Firefox 1.0):


y = x


Error: x is not defined
Source File: file:///c:/temp/foo.js


ML
Standard ML
Standard ML is a general-purpose, modular, functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of theorem provers.SML is a modern descendant of the ML...

 (Standard ML of New Jersey v110.55):


val y = x;


stdIn:1.9 Error: unbound variable or constructor: x


MUMPS
MUMPS
MUMPS , or alternatively M, is a programming language created in the late 1960s, originally for use in the healthcare industry. It was designed for the production of multi-user database-driven applications...




Set Y=X





OCaml 3.08

let y = x;;


Unbound value x


Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 5.8:

my $y = $x;


(no error)



use strict;
my $y = $x;


Global symbol "$x" requires explicit package name at foo.pl line 2.
Execution of foo.pl aborted due to compilation errors.


PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...

 5:

$y = $x;


(no error)



error_reporting(E_ALL);
$y = $x;


PHP Notice: Undefined variable: x in foo.php on line 3


Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 2.4:

x = y


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


Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...

 1.8

y = x


NameError: undefined local variable or method `x' for main:Object
from (irb):1


VBScript
VBScript
VBScript is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It is designed as a “lightweight” language with a fast interpreter for use in a wide variety of Microsoft environments...

(WSH 5.6)

Dim y
y = x


(no error)



Option Explicit

Dim y
y = x


(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK