Target Language Compiler | ![]() ![]() |
The target language function construct is
%function identifier ( optional-arguments ) [Output | void] %return %endfunction
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 the Target Language Values table.
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 name(x,y,z) void %if x == y %return x %else %return z %endif %endfunction
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
%function foo() Output ... %endfunction
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
%function foo() void ... %endfunction
Variable Scoping Within Functions
Within a function, the left-hand member of any %assign
statement defaults to create a new entry in the function's block within the scope chain, and does not affect any of the other entries. That is, it is local to the function. For example,
%function foo (x,y) %assign local = 3 %endfunction
adds local = 3
to the foo( )
block in the scope list giving
Figure 5-7: Scoping Rules Within Functions Containing Local Variables
You can override this default behavior by using %assign
with the ::
operator. For example,
%assign ::global = 3
makes global
a global variable and initializes it to 3.
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.
Figure 5-8: 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.
%function foo(s) %with s %return(name) %endwith %endfunction
The %return
statement does not require a value. You can use %return
to return from a function with no return value.
![]() | Scoping | Command Line Arguments | ![]() |