Undefined variable

1

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. 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 " " or Visual Basic's " ").

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

*** - EVAL: variable X has no value

C

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

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. ReferenceError: x is not defined Source File: file:///c:/temp/foo.js

Lua

(no error, continuing) nil

ML (Standard ML of New Jersey)

stdIn:1.9 Error: unbound variable or constructor: x

MUMPS

Set Y=X <UNDEF>

OCaml

Unbound value x

Perl

(no error)

PHP 5

(no error) PHP Notice: Undefined variable: x in foo.php on line 3

Python

Python 3

Python 2.4

REXX

+++ Error 30 in line 2: Label not found

Ruby

Tcl

VBScript

(no error) (3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article