# Circular reference

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

Series of references where the last object references the first

This article is about a series of references where the last object references the first. For Multiple sources for single-source data, see [Circular reporting](/source/Circular_reporting).

For the Wikipedia policy in regard to reliable sources, see [Wikipedia:Circular reference](https://en.wikipedia.org/wiki/Wikipedia:Circular_reference)

A **circular reference** (or **reference cycle**[1]) is a series of [references](/source/Reference) where the last object references the first, resulting in a closed loop.

Circular reference (in red)

## Simple example

A newcomer asks a local where the town library is. "Just in front of the post office," says the local. The newcomer nods, and follows up: "But where is the post office?"

"Why, that's simple," replies the local. "It's just behind the library!"

## In language

A circular reference is not to be confused with the [logical fallacy](/source/Logical_fallacy) of a [circular argument](/source/Circular_argument). Although a circular reference will often be unhelpful and reveal no information, such as two entries in a book index referring to each other, it is not necessarily so that a circular reference is of no use. Dictionaries, for instance, must always ultimately be a circular reference since all words in a dictionary are defined in terms of other words, but a dictionary nevertheless remains a useful reference. Sentences containing circular references can still be meaningful:

- *Her brother gave her a kitten; his sister thanked him for it.*

is circular, but not without meaning. Indeed, it can be argued that self-reference is a necessary consequence of Aristotle's [law of non-contradiction](/source/Law_of_non-contradiction), a fundamental philosophical [axiom](/source/Axiom). In this view, without self-reference, [logic](/source/Logic) and [mathematics](/source/Mathematics) become impossible, or at least, lack usefulness.[2][3]

## In computer programming

For circular references between objects or resources, see [Reference counting](/source/Reference_counting).

Circular references can appear in [computer programming](/source/Computer_programming) when one piece of code requires the result from another, but that code needs the result from the first. For example, the two functions, posn and plus1 in the following Python program comprise a circular reference:[*[further explanation needed](https://en.wikipedia.org/wiki/Wikipedia:Please_clarify)*]

def posn(k: int) -> int:
    if k < 0:
        return plus1(k)
    return k

def plus1(n: int) -> int:
    return posn(n + 1)

Circular references like the above example may return valid results if they have a terminating condition. If there is no terminating condition, a circular reference leads to a condition known as [livelock](/source/Livelock) or [infinite loop](/source/Infinite_loop), meaning it theoretically could run forever.

def posn(k: int) -> int:
    return plus1(k)

def plus1(n: int) -> int:
    return posn(n + 1)

In ISO Standard, SQL circular integrity constraints are implicitly supported within a single table. Between multiple tables circular constraints (e.g. foreign keys) are permitted by defining the constraints as deferrable (See [CREATE TABLE](http://www.postgresql.org/docs/current/static/sql-createtable.html) for PostgreSQL and [DEFERRABLE Constraint Examples](http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm#i1015767) for Oracle). In that case the constraint is checked at the end of the transaction not at the time the DML statement is executed. To update a circular reference, two statements can be issued in a single transaction that will satisfy both references once the transaction is committed.

Circular references can also happen between instances of data of a mutable type, such as in this Python script:

mydict: dict[str, str] = {
  "this": "that",
  "these": "those"
}
mydict["myself"] = mydict
print(mydict)

The print(mydict) function will output {'this': 'that', 'these': 'those', 'myself': {...}}, where {...} indicates a circular reference, in this case, to the mydict dictionary.

## In spreadsheets

Circular references also occur in [spreadsheets](/source/Spreadsheet) when two cells require each other's result. For example, if the value in Cell A1 is to be obtained by adding 5 to the value in Cell B1, and the value in Cell B1 is to be obtained by adding 3 to the value in Cell A1, no values can be computed. (Even if the specifications are A1:=B1+5 and B1:=A1-5, there is still a circular reference. It does not help that, for instance, A1=3 and B1=-2 would satisfy both formulae, as there are infinitely many other possible values of A1 and B1 that can satisfy both instances.)

Circular reference in worksheets can be a very useful technique for solving implicit equations such as the [Colebrook equation](/source/Colebrook_equation) and many others, which might otherwise require tedious [Newton-Raphson](/source/Newton-Raphson) algorithms in VBA or use of macros.[4]

A distinction should be made with processes containing a circular reference between those that are incomputable and those that are an iterative calculation with a final output. The latter may fail in spreadsheets not equipped to handle them but are nevertheless still logically valid.[3]

## See also

- [Causality](/source/Causality) – How one process influences another

- [Circular reporting](/source/Circular_reporting) – Apparently multiple sources for single-source data

- [Citogenesis](/source/Citogenesis) – Circular reporting on Wikipedia

- [Halting problem](/source/Halting_problem) – Problem in computer science

- [MS Fnd in a Lbry](/source/MS_Fnd_in_a_Lbry) – Short story by Hal Draper

- [Nested function](/source/Nested_function) – Named function defined within a function

- [Quine (computing)](/source/Quine_(computing)) – Self-replicating program

- [Regress argument](/source/Regress_argument) – Philosophical problemPages displaying short descriptions of redirect targets

- [Self-reference](/source/Self-reference) – Sentence, idea or formula that refers to itself

- [There's a Hole in My Bucket](/source/There's_a_Hole_in_My_Bucket) – Classic, humorous children's song

## References

1. **[^](#cite_ref-rustrefcycle_1-0)** Klabnik, Steve; Nichols, Carol. ["Reference Cycles Can Leak Memory"](https://doc.rust-lang.org/book/ch15-06-reference-cycles.html). *The Rust Programming Language*. Rust Team. Retrieved 8 November 2023.

1. **[^](#cite_ref-2)** [Terry A. Osborn](/source/Terry_A._Osborn), [*The future of foreign language education in the United States*](https://books.google.com/books?id=fvJTwwCSy8UC&dq=%22circular+reference%22+-excel+-computer+-programming&pg=PA32), pp.31-33, Greenwood Publishing Group, 2002 [ISBN](/source/ISBN_(identifier)) [0-89789-719-6](https://en.wikipedia.org/wiki/Special:BookSources/0-89789-719-6).

1. ^ [***a***](#cite_ref-Fiengo_3-0) [***b***](#cite_ref-Fiengo_3-1) Robert Fiengo, Robert May, [*Indices and identity*](https://books.google.com/books?id=V77kaAVpU0UC&dq=%22circular+reference%22+-excel+-computer+-programming&pg=PA60), pp.59-62, MIT Press, 1994 [ISBN](/source/ISBN_(identifier)) [0-262-56076-3](https://en.wikipedia.org/wiki/Special:BookSources/0-262-56076-3).

1. **[^](#cite_ref-4)** ["Solve Implicit Equations Inside Your Worksheet By Anilkumar M, Dr Sreenivasan E and Dr Raghunathan K"](https://web.archive.org/web/20090817224237/http://www.frpdesigns.com/iteration.htm). Archived from [the original](http://www.frpdesigns.com/iteration.htm) on 2009-08-17. Retrieved 2009-06-25..

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