Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Nested Functions

Part of TutorialFundamentals

Description

Nested functions allows you to structure functions in a clever way if that's how your mind works.

Example

import std.stdio;

int main(char[][] Args) {
  int i;

  void runIt() {
    /* This is a nested function. You can make use of variables declared in the outer
       function, but each has to be declared above the location of the nested function
       if you use it.*/

    writef("%d\tdo something\t", i);
  }

  bool found;
  while (!found) {
    if(i == 0) {
        writef("not found\t");
        runIt();
        return 0;
    }
    found = true;
  }
  return 0;
}

Testing

Tested using Digital Mars D Compiler v1.026 on Windows 2000.

More Information

See also the D Specification.