Target Language Compiler    

Target Language Functions

The target language function construct is

Functions in the target language are recursive and have their own local variable space. Target language functions do not produce any output, unless they explicitly use the %openfile, %selectfile, and %closefile directives, or are output functions.

A function optionally returns a value with the %return directive. The returned value can be any of the types defined in Table 6-2, Target Language Values.

In this example, a function, name, returns x, if x and y are equal, and returns z, if x and y are not equal:

Function calls can appear in any context where variables are allowed.

All %with statements that are in effect when a function is called are available to the function. Calls to other functions do not include the local scope of the function, but do include any %with statements appearing within the function.

Assignments to variables within a function always create new, local variables and can not change the value of global variables unless you use the scope resolution operator (::).

By default, a function returns a value and does not produce any output. You can override this behavior by specifying the Output and void modifiers on the function declaration line, as in

In this case, the function continues to produce output to the currently open file, if any, and is not required to return a value. You can use the void modifier to indicate that the function does not return a value, and should not produce any output, as in

Variable Scoping Within Functions

Within a function, the left-hand member of any %assign statement defaults to create a local variable. A new entry is created in the function's block within the scope chain; it does not affect any of the other entries. An example is shown in Figure 6-2.

You can override this default behavior by using %assign with the scope resolution operator (::).

When you introduce new scopes within a function using %with, these new scopes are used during nested function calls, but the local scope for the function is not searched. Also, if a %with is included within a function, its associated scope is carried with any nested function call, as shown in Figure 6-5, Scoping Rules When Using %with Within a Function,.

Figure 6-5: Scoping Rules When Using %with Within a Function

%return

The %return statement closes all %with statements appearing within the current function. In this example, the %with statement is automatically closed when the %return statement is encountered, removing the scope from the list of searched scopes:

The %return statement does not require a value. You can use %return to return from a function with no return value.


  Variable Scoping Command Line Arguments