Share on Facebook Share on Twitter Email
Answers.com

Undefined variable

 
Computer Desktop Encyclopedia: undefined variable

A variable (structure that holds data) that is not available at runtime to the application. This typically occurs with programming languages such as scripts, which allow variables to be located at runtime rather than being resolved ahead of time in a compile stage. See variable.

Download Computer Desktop Encyclopedia to your iPhone/iTouch

Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
Wikipedia: Undefined variable
Top

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.

In some programming languages an implicit declaration is provided the first time such a variable is encountered at compile time or run time error. In other languages such a usage is considered to be a fatal error, resulting an a diagnostic being issued.

Some languages have started out with the implicit declaration 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

Examples of how various programming language implementations 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

<UNDEF>

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.

PHP 5:

$y = $x;

(no error)

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



 
 
Learn More
500 error (technology)
variable (technology)
MIIS (programming language)

What is undefined in algebra? Read answer...
What is an undefined term? Read answer...
What are undefined Slopes? Read answer...

Help us answer these
Why are points undefined?
What is a undefined region?
An undefined term of geometry?

Post a question - any question - to the WikiAnswers community:

 

Copyrights:

Computer Desktop Encyclopedia. THIS DEFINITION IS FOR PERSONAL USE ONLY.
All other reproduction is strictly prohibited without permission from the publisher.
© 1981-2010 The Computer Language Company Inc.  All rights reserved.  Read more
Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Undefined variable" Read more