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 another type systems an undefined value may be unknown, unpredictable value, or merely a program failure on attempt of its evaluation.
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.
Even some mathematically well-defined expressions like exp(100000) may be undefined in floating point arithmetic because exceed its limits.
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.
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 runtime undefined value. 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 reading memory from a garbage pointer can crash a program.
![]() | This section needs expansion. You can help by adding to it. |
Notation
In computability theory, undefinedness of an expression is denoted as expr↑, and definedness as expr↓.
See also
- Defined and undefined (mathematics)
- Null (SQL)