Nested Functions

DXL supports nested functions. In other words, you can declare functions within a function. However, local variables declared within the parent function are not available to the child function. In this respect, DXL does not support nesting as you might expect. But one child function can call other child functions as long as they are in scope.

A possible use for this is if you wanted to make a function globally available, but did not want to allow direct access to call the other functions it calls. In the following example, harry() and sally() are hidden outside of printNames().

The following example demonstrates how this works. This code runs without error. Notice that function sally() can call harry(), but harry cannot call sally() because the latter is out of scope (i.e. declared after the former).

void printNames()
	string localVar = ""

	string harry()
		// this won't work - no access to local variable...

		// ...nor will this because sally is out of scope


	string sally()
		string sue()

		// this works because harry() is in scope
		return("Sally. Their names are " harry " and " sue ".")

	// nested functions are in scope
	print("My name is " harry) "n"
	print("My name is " sally) "n"