Nested Functions |
Scroll |
Nested Functions are common in all programming languages and syntactically nothing more than a function defined within a function where the result of an inner function becomes an input parameter to the parent function. Nesting functions within other functions provides total flexibility and control of the logical flow and data transformations possible in an Apply Engine script. Functions are processed internally as a "stack" and it is helpful to use parenthesis to improve the clarity of the evaluation sequence.
Example 1
The first example uses both a conditional flow function and nested functions. If the current time of day is 12:00 or later then it is said to be 'PM' which is the abbreviation of the Latin "Post Meridiem" and a variable V_AM_or_PM could be set as follows:
if (LEFT(TIME(),2) > 12)
V_AM_or_PM = 'PM'
else
V_AM_or_PM = 'AM'
Example 2
The following example shows two (2) levels of nesting within a CASE function. Set the variable V_WORKVAR to ‘CONTRACT’ if the WORKER_TYPE field contains a ‘C’ or the value ‘EMPLOYEE’ if the WORKER_TYPE column contains an ‘E’. If the WORKER_TYPE column does not contain a ‘C’ or ‘E’, the variable TEMP_VAR is set to the value ‘MISSING’.
CASE
WHEN WORKER_TYPE = 'C'
V_WORKVAR = STRING('CONTRACT')
WHEN WORKER_TYPE = 'F'
V_WORKVAR = STRING('EMPLOYEE')
OTHERWISE
V_WORKVAR = STRING('MISSING')
Example 3
This example introduces another level of nesting to the previous by adding an OR condition to the CASE logic.
CASE
WHEN (WORKER_TYPE = 'C' OR WORKER_TYPE = 'E')
{
V_WORKVAR = 'KNOWN')
}
OTHERWISE
{
V_WORKVAR = 'MISSING'
}