Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Examples  



1.1  CLISP  





1.2  C





1.3  JavaScript  





1.4  Lua  





1.5  ML (Standard ML of New Jersey)  





1.6  MUMPS  





1.7  OCaml  





1.8  Perl  





1.9  PHP 5  





1.10  Python  



1.10.1  Python 3  





1.10.2  Python 2.4  







1.11  REXX  





1.12  Ruby  





1.13  Tcl  





1.14  VBScript  







2 References  














Undefined variable






Tiếng Vit
 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


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

[edit]

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

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

C

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

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.

"use strict";
let y = x;
let y = x;
let x; // causes error on line 1
 ReferenceError: x is not defined
 Source File: file:///c:/temp/foo.js
[edit]
y = x

(no error, continuing)

print(y)
nil

ML (Standard ML of New Jersey)

[edit]
val y = x;
stdIn:1.9 Error: unbound variable or constructor: x
[edit]
Set Y=X
<UNDEF>
[edit]
let y = x;;
Unbound value x
[edit]
my $y = ($x // 0) + 1; # defined-or operator
(no error)

PHP5

[edit]
$y = $x;
(no error)
$y="";
$x="";
error_reporting(E_ALL);
$y = $x;
PHP Notice:  Undefined variable: x in foo.php on line 3
[edit]

Python 3

[edit]
x = y
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    x = y
NameError: name 'y' is not defined

Python 2.4

[edit]
>>> x = y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined
[edit]
signal on novalue
y = x
+++ Error 30 in line 2: Label not found
[edit]
irb(main):001:0> y = x
NameError: undefined local variable or method `x' for main:Object
 from (irb):1
[edit]
% set y $x
can't read "x": no such variable
[edit]
Dim y
y = x
(no error)
Option Explicit

Dim y
y = x
(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'

References

[edit]
  1. ^ "undefined variable." YourDictionary, n.d. Web. 24 July 2013. <http://computer.yourdictionary.com/undefined-variable>.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Undefined_variable&oldid=1208225022"

Categories: 
Variable (computer science)
Software bugs
 



This page was last edited on 16 February 2024, at 20:31 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki