Undefined value


In computing, 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" 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 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 its 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 may be undefined in floating point arithmetic because the result is so large that it cannot be represented. If an implementation supports ±∞, then this value may be computed as +∞, however.
An element of an array is undefined when its index is out of bounds, as is the value in an associative array for a key which it does not contain.
An argument of a variadic function which was not passed to it, is undefined within the function body.
A variable which is not initialized has undefined value until it is assigned.
Dereferences of null pointers lead to undefined values 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 may be seen as undefined too, but is of only theoretical interest because such a function never returns.

Treatment

In Perl language, definedness of an expression can be checked via predicate defined. The use of undefined value in Perl is quite safe, it is equivalent to false in logical context.
In such statically typed languages as C there is no specific notion of a value undefined at runtime. Arithmetically undefined expressions invoke exceptions and crash the program, if uncaught. Undefined 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 must be initialized before it can be used.

Notation

In computability theory, undefinedness of an expression is denoted as expr↑, and definedness as expr↓.