Nested functions

Nested functions have full access to all the variables and functions that are available in the external function scope.
If the nested function is used as a constructor then a new turtle-object has access to all the functions available in the external function scope, however this is not the case of inheritance from the class described with the external function:
- external function is not executed as a base class constructor;
- local and global variables created in the external function scope are not available to the new turtle.

Note: all functions that are defined in the code are nested in the main function of the #first turtle.

Example 1:
A call to the nested function f2: external functions and variables are available.

"x := 1
to f1 print "f1_external end

to test
  to f2
    print "f2_nested
    print :x
  end
  f1
  f2
end

test

Output:

f1_external
f2_nested
1

Example 2:
Nested function f2 is used as a constructor of a new turtle: external functions are available, however the new turtle has no access to the variable :x which was created outside of function test.

"x := 1
to f1 print "f1_external end

to test
  to f2
    print "f2_constructor
    ;print :x  ;error: variable :x is not available here
    f1
  end
  "t := newt $f2
end

test

Output:

f2_constructor
f1_external

See also:

Turtle - object

Table of Content