{
  "id": "enwiki-001371356922",
  "slug": "nested-function",
  "title": "Nested function",
  "qid": "",
  "lead": "In computer programming, a nested function (or nested procedure or subroutine) is a named function that is defined within another (enclosing) block and is lexically scoped within the enclosing block meaning it is only callable by name within the body of the enclosing block and can use identifiers declared in outer blocks, including outer functions. The enclosing block is typically, but not always, another function. Programming language support for nested functions varies. With respect to structured programming languages, it is supported in some outdated languages such as ALGOL, Simula 67 and Pascal and in the commonly used JavaScript. It is commonly supported in dynamic and functional languages. However, it is not supported in some commonly used languages including standard C and C++. Other programming technologies provide similar benefit. For example, a lambda function also allows for a function to be defined inside of a function (as well as elsewhere) and allows for similar data hiding and encapsulation. Notably, a lambda function has no name (is anonymous) and therefore cannot be called by name and has no visibility aspect.",
  "body": "In computer programming, a nested function (or nested procedure or subroutine) is a named function that is defined within another (enclosing) block and is lexically scoped within the enclosing block meaning it is only callable by name within the body of the enclosing block and can use identifiers declared in outer blocks, including outer functions. The enclosing block is typically, but not always, another function. Programming language support for nested functions varies. With respect to structured programming languages, it is supported in some outdated languages such as ALGOL, Simula 67 and Pascal and in the commonly used JavaScript. It is commonly supported in dynamic and functional languages. However, it is not supported in some commonly used languages including standard C and C++. Other programming technologies provide similar benefit. For example, a lambda function also allows for a function to be defined inside of a function (as well as elsewhere) and allows for similar data hiding and encapsulation. Notably, a lambda function has no name (is anonymous) and therefore cannot be called by name and has no visibility aspect. function E(x: real): real; function F(y: real): real; begin F := x + y end; begin E := F(3) + F(4) end; The function F is nested within E . Note that E 's parameter x is also visible in F (as F is a part of E ) while both x and y are invisible outside E and F respectively. Similarly, in Standard ML: fun e (x : real) = let fun f y = x+y in f 3 + f 4 end; In Haskell: e :: Float -> Float e x = f 3 + f 4 where f y = x + y In PL/I: In Python: def e(x: float) -> float: def f(y: float) -> float: return x + y return f(3.0) + f(4.0) In GNU C which extends standard C with nested functions: float e(float x) { float f(float y) { return x + y; } return f(3.0f) + f(4.0f); } void sort(int* items, int size) { void quickSort(int first, int last) { void swap(int p, int q) { int tmp = items[p]; items[p] = items[q]; items[q] = tmp; } int partition() { int pivot = items[first]; int index = first; swap(index, last); for (int i = first; i The following is an implementation of the Hoare partition based quicksort using C++11 lambda expression syntax which is an alternative technology that also allows hiding a function inside a function: // Iter represents a random access iterator template void sort(Iter begin, Iter end) { auto partition = [&]() -> Iter { // Hoare partition scheme Iter& pivot = *begin; Iter forwardCursor = begin; Iter backwardCursor = end - 1; Iter partitionPositionFound = false; auto locatePartitionPosition = [&]() -> void { while (*forwardCursor = backwardCursor) { partitionPositionFound = true; } else swap(*forwardCursor, *backwardCursor); } }; // Trivial helper function auto moveOnAndTryAgain = [&]() -> void { ++forwardCursor; --backwardCursor; }; // Brief outline of the actual partition process while (true) { locatePartitionPosition(); if (partitionPositionFound) return backwardCursor + 1; else { moveOnAndTryAgain(); } } }; // Brief outline of the quicksort algorithm if (begin Note that P is visible for, and may therefore be called by, C if (P) = C / (C) / ((C)) / etc. This original method is faster than it may seem, but it is nevertheless often optimized in practical modern compilers (using displays or similar techniques). Another way to implement nested functions that is used by some compilers is to convert (\"lift\") nested functions into non-nested functions (where extra, hidden, parameters replace the access links) using a process known as lambda lifting during an intermediate stage in the compilation.",
  "categories": [
    "129826521",
    "20416984",
    "23118434",
    "23603337",
    "62213919"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1371356922",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1787703083.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001371356922",
    "type": "webpage",
    "title": "Nested function",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/nested-function",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          11
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          11
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001371356922",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1371356922",
    "note": "Immutable snapshot enwiki-001371356922"
  }
}