Undefined value
In computing (particularly, in programming), undefined value is a condition where an expression does not have a correct value, although it is syntactically correct. An undefined value must not be confused with empty string, boolean "false" or other "empty" (but defined) values. Depending on circumstances, evaluation to an 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. For instance, 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
The 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 the result is so large it cannot be represented. If an implementation supports ±∞, then this value may be computed as +∞ (inf), however.
An element of an array is undefined when index is out of bounds, as is 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 within the function body.
A variable which is not initialized, has undefined (or unpredictable) value until it was assigned.
Dereferences of null pointers lead to undefined value and usually raise 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 poorly designed programs or as a result of an 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.
Undefined value and nullable types
A nullable data type reserves a special null
value for representing undefined value, so the null value becomes 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
- ↑ "undef". Perl 5 documentation. 2009-09-25. Retrieved 2010-03-26.
- ↑ "defined". Perl 5 documentation. 2009-09-25. Retrieved 2010-03-26.
- ↑ "C# Nullable Numeric Data Types". C# Fundamentals tutorial. Retrieved 2010-03-27.
See also
- Defined and undefined (mathematics)
- Null (SQL)