encyclopegia.org

Nested function

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.

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.

References

No reference rows published for this article.

Cite this article

MLA

"Nested function." encyclopegia.org, 2026, https://encyclopegia.org/wiki/nested-function. Accessed 23 November 2026.

Chicago

"Nested function." encyclopegia.org. Accessed November 23, 2026. https://encyclopegia.org/wiki/nested-function.

APA

encyclopegia.org. (2026). Nested function. Retrieved November 23, 2026, from https://encyclopegia.org/wiki/nested-function

Accessed November 23, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Role of Honour

enwiki · references available

Piedmont Geriatric Hospital

enwiki · references available

Pakil

enwiki · references available

Xinzheng

enwiki · references available

Amber MacArthur

enwiki · references available

Miguel Ángel Moratinos

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1371356922 · 0.5 · snapshot enwiki-001371356922

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.