Määrittelemätön muuttuja - Undefined variable

Määrittelemätön muuttuja on lähdekoodi on tietokoneohjelma on muuttuja , joka on näytetty koodin, mutta ei ole aiemmin ilmoitettu kyseisen koodin.

Joillakin ohjelmointikielillä implisiittinen ilmoitus annetaan ensimmäisen kerran, kun tällainen muuttuja havaitaan kääntämisajankohtana . Muilla kielillä tällaisen käytön katsotaan olevan riittävän vakava, jotta diagnoosi annetaan ja kokoaminen epäonnistuu.

Jotkut kielimäärittelyt käyttivät alun perin implisiittistä ilmoituskäyttäytymistä ja kypsyessään tarjosivat mahdollisuuden poistaa se käytöstä (esim. Perlin " use warnings" tai " Visual Basicen " " Option Explicit").

Esimerkkejä

Seuraavassa on joitain esimerkkejä siitä, miten erilaiset ohjelmointikieletoteutukset reagoivat määrittelemättömiin muuttujiin. Jokaisen koodinpätkän jälkeen seuraa virheilmoitus (jos sellainen on).

CLISP

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

C

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

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

Lua

y = x

(ei virhettä, jatkuu)

print(y)
nil

ML (New Jerseyn vakio ML)

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

SIKOTAUTI

Set Y=X
<UNDEF>

OCaml

let y = x;;
Unbound value x

Perl

my $y = ($x // 0) + 1; # defined-or operator
(no error)

PHP 5

$y = $x;
(no error)
$y="";
$x="";
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 "<stdin>", line 1, in <module>
NameError: name 'y' is not defined

REXX

signal on novalue
y = x
+++ Error 30 in line 2: Label not found

Rubiini

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

Tcl

% set y $x
can't read "x": no such variable

VBScript

Dim y
y = x
(no error)
Option Explicit

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

Viitteet