| 1 | = Nested Functions = |
---|
| 2 | |
---|
| 3 | ''Part of'' TutorialFundamentals |
---|
| 4 | |
---|
| 5 | == Description == |
---|
| 6 | |
---|
| 7 | Nested functions allows you to structure functions in a clever way if that's how your mind works. |
---|
| 8 | |
---|
| 9 | == Example == |
---|
| 10 | |
---|
| 11 | {{{ |
---|
| 12 | #!d |
---|
| 13 | import std.stdio; |
---|
| 14 | |
---|
| 15 | int main(char[][] Args) { |
---|
| 16 | int i; |
---|
| 17 | |
---|
| 18 | void runIt() { |
---|
| 19 | /* This is a nested function. You can make use of variables declared in the outer |
---|
| 20 | function, but each has to be declared above the location of the nested function |
---|
| 21 | if you use it.*/ |
---|
| 22 | |
---|
| 23 | writef("%d\tdo something\t", i); |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | bool found; |
---|
| 27 | while (!found) { |
---|
| 28 | if(i == 0) { |
---|
| 29 | writef("not found\t"); |
---|
| 30 | runIt(); |
---|
| 31 | return 0; |
---|
| 32 | } |
---|
| 33 | found = true; |
---|
| 34 | } |
---|
| 35 | return 0; |
---|
| 36 | } |
---|
| 37 | }}} |
---|
| 38 | |
---|
| 39 | == Testing == |
---|
| 40 | |
---|
| 41 | Tested using Digital Mars D Compiler v1.026 on Windows 2000. |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | == More Information == |
---|
| 45 | |
---|
| 46 | See also the [http://www.digitalmars.com/d/1.0/function.html#nested D Specification]. |