Example: orders.4gl (Function comp_order_total)
This function implements the SQL row deletion for an order item row.
Function
comp_order_total
(orders.4gl): 1 PRIVATE FUNCTION comp_order_total() RETURNS ()
2 DEFINE x INTEGER
3 LET order_total = 0
4 FOR x = 1 TO orditems.getLength()
5 LET order_total = order_total + NVL(orditems[x].line_total,0)
6 END FOR
7 END FUNCTIONNote:
- Line
1declares the function without parameter and without return value. - Line
2defines thexinteger variable, used as array index in theFORloop. - Line
3initializes the total variable to zero. - Lines
4to8implement theFORloop to scan the rows of theorditemsdynamic array. - Line 5 adds the value of the current order line to the total. Note the use of the
NVL()operator: ThisNVL()expression will return to zero, if the line_total field isNULL. AddingNULLthe the order_total variable would result toNULL.