ArticleslgStudy

computer science

Undefined value

Undefined value is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Undefined value rather than just read about it. In short: In computing (particularly, in programming), undefined value is a condition where an expression in a programming language does not have a semantically correct value in the language, even though it is syntactically correct. An undefined value must not be confused with empty string, Boolean "false" or other "empty" (but defined) values.

Key takeaways

  • Undefined value belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Undefined value to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Undefined value from memory before moving on to harder problems.

Reference excerpt

In computing (particularly, in programming), undefined value is a condition where an expression in a programming language does not have a semantically correct value in the language, even though 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 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 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. Another common example is accessing an array with an index which is out of bounds, as is the value in an associative array for a key which it does not contain. There are various ways that these situations are handled in practice:

Reserved value

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 reserves the special value EOF to indicate that no more input is available. The getchar() function returns the next available input character, or EOF if there is no more available. (The ASCII character code defines a 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 EOF value.) The IEEE 754 floating-point arithmetic standard defines a special "not a number" value which is returned when an arithmetic operation has no defined value. Examples are division by zero, or the square root or logarithm of a negative number. Structured Query Language has a special NULL value to indicate missing data. The Perl language lets the definedness of an expression be checked via the defined() predicate. Many programming languages support the concept of a 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#. Most Unix system calls return the special value −1 to indicate failure. While dynamically typed languages often ensure that uninitialized variables 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).

Exception handling Some programming languages have a concept of 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, 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

A function which never returns has an undefined value because the value can never be observed. Such functions are formally assigned the bottom type, which has no values. Examples fall into two categories:

Functions which loop forever. 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 in a partial recursive function.) Functions which terminate the computation, such as the 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

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; 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" and "make demons fly out of your nose".) The classic example is a dangling pointer reference. It is very fast to dereference a valid pointer, but can be very complex to determine if a pointer is valid. Therefore, computer hardware and low-level languages such as C do not attempt to validate pointers before dereferencing them, instead passing responsibility to the programmer. This offers speed at the expense of safety.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Undefined value

Start with the simplest possible case. Write down what Undefined value claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Undefined value before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Undefined value ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Undefined value

In research
Undefined value appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Undefined value in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Undefined value is common in secondary-school and first-year university syllabi. It links to neighbouring topics Software anomalies, Theory of computation, so understanding it makes those chapters shorter.
In everyday life
Look for Undefined value outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Undefined value in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Undefined value means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Undefined value out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Undefined value in simple terms?

In computing (particularly, in programming), undefined value is a condition where an expression in a programming language does not have a semantically correct value in the language, even though it is syntactically correct. An undefined value must not be confused with empty string, Boolean "false" o…

Why does Undefined value matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Undefined value?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Undefined value.

Tags

  • Software anomalies
  • Theory of computation

Keep exploring