# Undefined value

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Undefined_value
> Markdown URL: https://mediated.wiki/source/Undefined_value.md
> Source: https://en.wikipedia.org/wiki/Undefined_value
> Source revision: 1328981578
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{short description|In computing, a condition where an expression does not have a correct value}}
{{Other uses|Undefined (disambiguation){{!}}Undefined}}
{{Refimprove|date=March 2010}}
{{Use dmy dates|date=December 2021|cs1-dates=y}}
In computing (particularly, in [programming](/source/Computer_programming)), '''undefined value''' is a condition where an [expression](/source/expression_(programming)) in a [programming language](/source/programming_language) does not have a [semantically](/source/semantics_(computer_science)) correct [value](/source/value_(computer_science)) in the language, even though it is [syntactically](/source/syntax_(programming_languages)) correct. An undefined value must not be confused with [empty string](/source/empty_string), [Boolean](/source/Boolean_data_type) "false" or other "empty" (but defined) values. Depending on circumstances, evaluation to an undefined value may lead to [exception](/source/exception_(programming)) or [undefined behaviour](/source/undefined_behaviour), but in some [programming language](/source/programming_language)s undefined values can occur during a normal, predictable course of [program](/source/program_(computing)) execution.

[Dynamically typed](/source/Dynamic_typing) languages usually treat undefined values explicitly when possible. For instance, [Perl](/source/Perl) has <code>undef</code> operator<ref name="Perl_2009_Undef"/> 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.

== Handling ==
The value of a [partial function](/source/partial_function) is undefined when its argument is out of its [domain of definition](/source/domain_of_definition). This include numerous [arithmetic](/source/arithmetic)al cases such as [division by zero](/source/division_by_zero), [square root](/source/square_root) or [logarithm](/source/logarithm) of a [negative number](/source/negative_number) etc. Another common example is accessing an [array](/source/array_data_structure) with an index which is out of bounds, as is the value in an [associative array](/source/associative_array) for a key which it does not contain. There are various ways that these situations are handled in practice:

=== Reserved value ===
{{main|Nullable type|Sentinel value|NaN}}

In applications where undefined values must be handled gracefully, it is common to reserve a special null value which is distinguishable from normal values. This resolves the difficulty by creating a defined value to represent the formerly undefined case.  There are many examples of this:
* The C [standard I/O library](/source/C_file_input%2Foutput) reserves the special value <code>[EOF](/source/End-of-file)</code> to indicate that no more input is available.  The <code>getchar()</code> function returns the next available input character, or <code>EOF</code> if there is no more available. (The [ASCII](/source/ASCII) character code defines a [null character](/source/null_character) for this purpose, but the standard I/O library wishes to be able to send and receive null characters, so it defines a separate <code>EOF</code> value.)
* The [IEEE 754](/source/IEEE_754) floating-point arithmetic standard defines a special "[not a number](/source/not_a_number)" value which is returned when an [arithmetic](/source/arithmetic) operation has no defined value.  Examples are [division by zero](/source/division_by_zero), or the [square root](/source/square_root) or [logarithm](/source/logarithm) of a [negative number](/source/negative_number).
* [Structured Query Language](/source/Structured_Query_Language) has a special <code>[NULL](/source/Null_(SQL))</code> value to indicate missing data.
* The Perl language lets the definedness of an expression be checked via the <code>defined()</code> predicate.<ref name="Perl_2009_Defined"/>
* Many programming languages support the concept of a [null pointer](/source/null_pointer) distinct from any valid pointer, and often used as an error return.
* Some languages allow most types to be nullable, for example [C#](/source/C_Sharp_(programming_language)).<ref name="Carr_2006"/>
* Most [Unix](/source/Unix) [system call](/source/system_call)s return the special value &minus;1 to indicate failure.

While [dynamically typed](/source/dynamically_typed) languages often ensure that [uninitialized variable](/source/uninitialized_variable)s default to a null value, statically typed values often do not, and distinguish null values (which are well-defined) from uninitialized values (which are not).{{r|Carr_2006}}

=== Exception handling ===
Some programming languages have a concept of [exception handling](/source/exception_handling) for dealing with failure to return a value.  The function returns in a defined way, but it does not return a value, so there is no need to invent a special value to return.

A variation on this is [signal handling](/source/signal_handling), which is done at the operating system level and not integrated into a programming language.  Signal handlers can attempt some forms of recovery, such as terminating part of a computation, but without as much flexibility as fully integrated exception handling.

=== Non-returning functions ===
{{main|Bottom type}}

A function which never returns has an undefined value because the value can never be observed.  Such functions are formally assigned the [bottom type](/source/bottom_type), which has no values.  Examples fall into two categories:
* Functions which [loop forever](/source/infinite_loop).  This may arise deliberately, or as a result of a search for something which will never be found.  (For example, in the case of failed [μ operator](/source/%CE%BC_operator) in a [partial recursive function](/source/partial_recursive_function).)
* Functions which terminate the computation, such as the [<code>exit</code> system call](/source/Exit_(system_call)).  From within the program, this is indistinguishable from the preceding case, but it makes a difference to the invoker of the program.

=== Undefined behaviour ===
{{main|Undefined behaviour}}

All of the preceding  methods of handling undefined values require that the undefinedness be detected.  That is, the called function determines that it cannot return a normal result and takes some action to notify the caller.  At the other end of the spectrum, ''undefined behaviour'' places the onus on the caller to avoid calling a function with arguments outside of its domain.  There is no limit on what might happen.  At best, an easily detectable [crash](/source/Crash_(computing)); at worst, a subtle error in a seemingly unrelated computation.

(The formal definition of "undefined behaviour" includes even more extreme possibilities, including things like "[halt and catch fire](/source/Halt_and_Catch_Fire_(computing))" and "make demons fly out of your nose".<ref name="Catb"/>)

The classic example is a [dangling pointer](/source/dangling_pointer) reference. It is very fast to [dereference](/source/dereference) a valid [pointer](/source/pointer_(computer_programming)), but can be very complex to determine if a pointer is valid. Therefore, computer hardware and low-level languages such as [C](/source/C_(programming_language)) do not attempt to validate pointers before dereferencing them, instead passing responsibility to the programmer. This offers speed at the expense of safety.

=== Undefined value ''sensu stricto'' ===
The strict definition of an undefined value is a superficially valid (non-null) output which is meaningless but does not trigger undefined behaviour. For example, passing a negative number to the [fast inverse square root](/source/fast_inverse_square_root) function will produce a number. Not a very useful number, but the computation will complete and return ''something''.

Undefined values occur particularly often in hardware. If a wire is not carrying useful information, it still exists and has some voltage level. The voltage should not be abnormal (e.g. not a damaging [overvoltage](/source/overvoltage)), but the particular [logic level](/source/logic_level) is unimportant.

The same situation occurs in software when a [data buffer](/source/data_buffer) is provided but not completely filled. For example, the C library <code>[strftime](/source/strftime)</code> function converts a timestamp to human-readable form in a supplied output buffer. If the output buffer is not large enough to hold the result, an error is returned and the buffer's contents are undefined.

In the other direction, the [<code>open</code> system call](/source/open_(system_call)) in [POSIX](/source/POSIX) takes three arguments: a file name, some flags, and a file mode.  The file mode is only used if the flags include <code>O_CREAT</code>. It is common to use a two-argument form of <code>open</code>, which provides an undefined value for the file mode, when <code>O_CREAT</code> is omitted.

Sometimes it is useful to work with such undefined values in a limited way. The overall computation can still be well-defined if the undefined value is later ignored.

As an example of this, the C language permits converting a pointer to an integer, although the numerical value of that integer is undefined. It may still be useful for debugging, for comparing two pointers for equality, or for creating an [XOR linked list](/source/XOR_linked_list).

Safely handling undefined values is important in [optimistic concurrency control](/source/optimistic_concurrency_control) systems, which detect [race condition](/source/race_condition)s after the fact. For example, reading a [shared variable](/source/shared_variable) protected by [seqlock](/source/seqlock) will produce an undefined value ''before'' determining that a race condition happened. It will then discard the undefined data and retry the operation. This produces a defined result as long as the operations performed on the undefined values do not produce full-fledged undefined behaviour.

Other examples of undefined values being useful are [random number generator](/source/random_number_generator)s and [hash function](/source/hash_function)s. The specific values returned are undefined, but they have well-defined properties and may be used without error.

== Notation ==
In [computability theory](/source/computability_theory), undefinedness of an expression is denoted as ''expr''↑, and definedness as ''expr''↓.

== See also ==

{{columns-list|

* [Defined and undefined](/source/Defined_and_undefined) (mathematics)
* [Null (SQL)](/source/Null_(SQL))
* [Type theory](/source/Type_theory)
* [Unit type](/source/Unit_type)

}}

== References ==
{{Reflist|refs=
<ref name="Perl_2009_Undef">{{cite web |title=undef |date=2009-09-25 |work=Perl 5 documentation |url=http://perldoc.perl.org/functions/undef.html |access-date=2010-03-26}}</ref>
<ref name="Perl_2009_Defined">{{cite web |title=defined |date=2009-09-25 |work=Perl 5 documentation |url=http://perldoc.perl.org/functions/defined.html |access-date=2010-03-26}}</ref>
<ref name="Carr_2006">{{cite web |title=C# Nullable Numeric Data Types |author-first=Richard |author-last=Carr |date=2006-10-01 |work=C# Fundamentals tutorial |url=http://www.blackwasp.co.uk/CSharpNullableDataTypes.aspx |archive-url=https://web.archive.org/web/20070121094123/http://www.blackwasp.co.uk/CSharpNullableDataTypes.aspx |url-status=usurped |archive-date=21 January 2007 |access-date=2010-03-27}}</ref>
<ref name="Catb">{{cite web |title=Nasal demons |website=[Jargon File](/source/Jargon_File) |url=http://catb.org/jargon/html/N/nasal-demons.html |access-date=2014-06-12}}</ref>
}}

Category:Software anomalies
Category:Theory of computation

---
Adapted from the Wikipedia article [Undefined value](https://en.wikipedia.org/wiki/Undefined_value) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Undefined_value?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
