Navigation
  • Home
  • Recent
  • Most Active
  • Popular
  • Blog
  • Credits
  • RSS
  •   Interaction
  • Register
  • Statistics
  •   Help
  • Suggestions
  • Contact Us
  • How to Edit
  • Help



  • [Edit]



        Nested function
                    An example
                    Purpose
                    Languages
                        Functional languages
                    Implementaion
            See also

    top

    An example
    An example using Pascal syntax:
    function E(x: integer): integer

    function F(y: integer): integer
    begin
    F
    = x + y

    end

    begin
    E
    = F(3)

    end
    and the same example in C syntax:

    int E(int x)

    The function F is nested within E (note that x is visible in F, while y is invisible outside F).

    top

    Purpose
    Nested functions are a form of information hiding and are useful for dividing procedural tasks into subtasks which are only meaningful locally; it avoids cluttering other parts of the program with functions, variables, etc. unrelated to those parts. Nested functions therefore complements other structuring possibilities such as records and objects.

    In languages with nested functions, functions may normally also contain local constants, and types (in addition to local variables, parameters, and functions), encapsulated and hidden in the same nested manner. This may further enhance the code structuring possibilites.

    top

    Languages
    Well known languages supporting lexically nested functions include:

      Scripting languages, such as Python and Perl 6 (support to various degrees)
      There is also a C-related language with nested functions, the D language.

    top

    Functional languages
    In Scheme and most other functional programming languages, nested functions are a common way of implementing algorithms with loops in them. A simple (tail) recursive inner function is created, which behaves as the algorithm's main loop, while the outer function performs startup actions that only need to be done once. In more complex cases, a number of mutually recursive functions may be created as inner functions.

    top

    Implementaion
    There are several ways to implement nested procedures, but the classic way is:

    Any non-local object, X, is reached via access-links in the activation posts on the machine stack. The caller, C, assists the called procedure, P, by pushing a direct link to the latest activation of P's immediate lexical encapsulation, (P), prior to the call itself. P may then quickly find the right activation for a certain X by following a fixed number (P.depth - X.depth) of links (normally a small number).

    The caller creates this direct link by (itself) following C.depth - P.depth + 1 older links, leading up to the latest activation of (P), and then temporarily bridging over these with a direct link to that activation; the link later disapears toghether with P, whereby the older links beneath it, may come into use again.

    This original method is faster than it may seem, but it is nevertheless often optimized in practical compilers.

    Note that P is visible for, and may therefore by called by, C if (P) = C / (C) / ((C)) / etc.

    top

    See also


     
    Search more:
     

       
    Source Privacy License Download Contact Us Atlas
    Scientus.org Dictionary (Yet Another Wiki) RC : 1.39
    MIT OpenCourseWare
    This article is licensed under the GNU Free Documentation License [copyleft]. It uses material from the Wikipedia article "Nested function". link