Undefined value
This article needs additional citations for verification. (March 2010) |
In computing (particularly, in programming), undefined value is a condition where an expression has not a correct value, although it is syntactically correct. Undefined value may not be confused with empty string, boolean "false" or other "empty" (but defined) values. Depending on circumstances, evaluation to undefined value may lead to exception or undefined behaviour, but in some programming languages undefined values can occur during a normal, predictable course of program execution.
Dynamically typed languages usually treat undefined values explicitly (when possible), e.g. Perl has undef
operator[1] which can "assign" such value to a variable. In other type systems an undefined value can mean an unknown, unpredictable value, or merely a program failure on attempt of its evaluation. Nullable types offer an intermediate approach; see below.
Examples
Value of a partial function is undefined, when its argument is out of domain of definition. This include numerous arithmetical cases such as division by zero, square root or logarithm of a negative number etc.; see NaN.
Even some mathematically well-defined expressions like exp(100000) may be undefined in floating point arithmetic because exceed its limits. If implementation supports ±∞, then this value may be computed as +∞ (inf), however.
Element of an array is undefined when index is out of bounds, as a lookup in an associative array for a key which it does not contain.
An argument of variadic function, which was not passed to it, is undefined when function is called.
A variable which is not initialized, has undefined (or unpredictable) value until it was assigned.
Dereference of null pointer lead to undefined value and usually rises an exception immediately.
Any expression of the bottom type is undefined by definition, because that type has no values.
The value of a function which loops forever (for example, in the case of failed μ operator in a partial recursive function) may be seen as undefined too, but only of a theoretical interest because such function never returns.
Treatment
In Perl language, definedness of an expression can be checked via predicate defined(
expr)
.[2] The use of undefined value in Perl is quite safe, it is equivalent to false in logical context (under if etc.).
In such statically typed languages as C(C++) there is no specific notion of a value undefined at runtime. Arithmetically undefined expressions invoke exceptions and crash the program, if uncaught. Undefined (means, unpredictable) data in C and similar languages may appear in case of flawy designed program or unexpected fault, and represent a severe danger, particularly pointers to deallocated memory and null pointers to arrays or structures. Even an attempt to read a value, which a garbage pointer refers to, can crash a program.
![]() | This section needs expansion. You can help by adding to it. (March 2010) |
Undefined value and nullable types
A nullable data type reserves a special null
value for representing undefined value, so null value become a kind of value; note that undefined value generally is not. Unlike languages with dynamic typing, a variable of nullable type (as implemented in C#) must be initialized before it can be used.[3]
Notation
In computability theory, undefinedness of an expression is denoted as expr↑, and definedness as expr↓.
References
See also
- Defined and undefined (mathematics)
- Null (SQL)