Examples of Calling a Formula from a Formula

Use these examples to understand how to call a formula from another formula using these methods:

  • A series of separate calls

  • A single self-contained call

The examples include two versions of a wage formula, and a table comparing the two methods using a few use cases.

The first two examples show different versions of the wage formula. These points apply to both examples:

  • The formula calls RATE_FORMULA to get a value for HOURLY_RATE.

  • The RATE_FORMULA enters text to call UNIT.

  • The formula call sets to enter the UNIT to 'Hourly'.

  • The RATE_FORMULA returns the rate in the output variable called RATE.

  • The GET_OUTPUT call returns 0.00 if the RATE_FORMULA doesn't return RATE.

Wage Formula Using Separate Calls

This example shows how to call a formula using separate calls.

SET_INPUT('UNIT', 'Hourly')
EXECUTE('RATE_FORMULA')
HOURLY_RATE = GET_OUTPUT('RATE',0.0)
WAGE = HOURS_WORKED * HOURLY_RATE
RETURN WAGE

Wage Formula Using a Self-Contained Call

This example shows how to call a formula using a self-contained call.

CALL_FORMULA 
('RATE_FORMULA','Hourly' > 'UNIT' 
/* SET_INPUT('UNIT', 'Hourly') */
,HOURLY_RATE < 'RATE' DEFAULT 0.0 
/* HOURLY_RATE = GET_OUTPUT('RATE',0.0) */
)
WAGE = HOURS_WORKED * HOURLY_RATE
RETURN RATE

Sample Expressions to Compare Methods

Here's what you can do using sample expressions in the two methods:

  • Call a formula

  • Set inputs and context values

  • Unset context values

  • Get output values into a variable or array variable

  • Provide a default output value

Note: SET_INPUT or > statements have no effect if the calling formula has no formula input or context of the same name.

This table shows a few use cases that compare the two methods using sample expressions.

Use Case

Using Separate Calls

Using a Self-Contained Call

Execute a formula where the formula GET_RATES is executed

EXECUTE('GET_RATES')

Use within a CALL_FORMULA statement

'GET_RATES'

Set an input value in the called formula where you round off EXTRA_HOURS to 2 decimal places and set the input OVERTIME in the called formula.

SET_INPUT
('OVERTIME'
,ROUNDUP(EXTRA_HOURS,2)
)

Use within a CALL_FORMULA statement

ROUNDUP(EXTRA_HOURS,2) >'OVERTIME'

Leave a formula input value unset inside the called formula, where RATE isn't a formula context.

A SET_INPUTS statement isn't required, but you can this:

SET_INPUT('RATE')

A SET statement isn't required, but you can use this:

> 'RATE'

Inherit a context value from the called formula.

For example, both the calling formula and the called formula support the AREA1 context. The called formula inherits the AREA1 context value from the calling formula.

No statements are required to do this.

No statements are required to do this.

Set a context value inside a called formula, where the called formula supports the AREA1 context and you must set AREA1 to 'London' in the called formula.

SET_INPUT
('AREA1'
,'London'
)
'London' > 'AREA1'

Call a formula with an unset context value, where the called formula supports the AREA1 context and AREA1 has to be unset in the called formula.

SET_INPUT('AREA1')
> 'AREA1'

Get a formula output from the called formula.

Get BONUS_RATE output value into the RATE variable using the default value 0.0 if the BONUS_RATE output doesn't exist or wasn't set.

RATE = 
GET_OUTPUT
('BONUS_RATE'
,0.0
)
RATE <'BONUS_RATE' DEFAULT 0.0

Get a formula output from a called formula into an array

Get the BONUS_RATE output value into the RATES array variable at index position 'BONUS'. Use the default value 0.0 if the BONUS_RATE output doesn't exist or wasn't set.

RATES['BONUS'] = 
GET_OUTPUT
('BONUS_RATE'
,0.0
)
RATES['BONUS'] <'BONUS_RATE' DEFAULT 0.0