Jump to content

Undefined value

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jainayush95 (talk | contribs) at 05:39, 11 March 2019 (References). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computing, an undefined value is the value of an expression (such as a variable) that is outside the range of expected values, even though the code operating on it is syntactically correct.

An undefined value differs from the empty string, Boolean "false" or other "empty", but well-defined, values.

Attempts to operate on an undefined value may lead to an exception being raised or undefined behavior. Some programming languages prescribe specific behavior when undefined values are encountered in the course of program execution.

Languages may define "undefined value" explicitly and include operations to test for them or assign them. For instance, Perl has undef operator[1] which can be assigned to a variable, and function defined to test for it; the undefined value can also propagate through expressions. In other systems an undefined value can simply be an unknown, unpredictable value, or cause a program failure on evaluation.

Some languages have nullable types which allow any type, including to be extended with a "null" value.

Examples

The value of a partial function is undefined when its argument is not in the domain of definition. Many arithmetical functions have limited real-number domains, such as division by zero, and the square root and logarithm of a negative number. This may sometimes be represented as not-a-number.

A well-defined value like exp(exp(100000)) may be too large for the floating point representation being used. This value may be represented as ∞ (inf), or be undefined.

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.

If a parameter to a variadic function is omitted at the point of call, its value is undefined within the execution context of the function.

A variable which is not initialized may have an undefined value until it is assigned. But many languages automatically initialise some or all variables to a well-defined value, if they are not explicitly iniitialsed at the point of declaration.

Dereferencing a null pointer may lead to undefined behavior.

Any expression of the bottom type is undefined by definition.

The value of a function which loops forever (for example, in the case of a failed μ operator in a partial recursive function) may be seen as undefined too, but is of only theoretical interest because such a function never returns.

Treatment

In Perl , 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 C++ there is no specific notion of an undefined value, but using uninitialzed variables, or references to nonexistent objects, generally leads to undefined behavior. Even an attempt to read an uninitialized object is undefined behaviour.

Nullable types

In C#), the keyword null is used to represent the undefined value. Any type can be augmented to be able to represent this value: such types are called nullable types. A variable of nullable type 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

UndefinedVsNull

  1. ^ "undef". Perl 5 documentation. 2009-09-25. Retrieved 2010-03-26.
  2. ^ "defined". Perl 5 documentation. 2009-09-25. Retrieved 2010-03-26.
  3. ^ "C# Nullable Numeric Data Types". C# Fundamentals tutorial. Retrieved 2010-03-27.

See also