Undefined variable

From Wikipedia, the free encyclopedia

An undefined variable in a computer program is a variable that is accessed by the program but which has not been previously declared by that program. This often results in a compile time or run time error such as undefined variable or undeclared variable.

Some languages do not trigger this error but rather implicitly declare the variable. This may be convenient (such as for short programs), but it can also be error-prone since typos inadvertently introduce new variables without warning. Some languages have started out with this behavior but as they matured they provided an option to disable it (e.g. Perl's "use strict" or Visual Basic's "Option Explicit").

Examples of how various programming languages respond to undefined variables are given below. Each code example is followed by an error message (if any).


CLISP (GNU CLISP 2.35):

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

C (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 (Mozilla Firefox 1.0):

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

ML (Standard ML of New Jersey v110.55):

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

MUMPS

Set Y=X

OCaml 3.08

let y = x;;
Unbound value x

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

Python 2.4:

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

Ruby 1.8

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

VBScript (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'
Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.