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 FUNCTION
Note:
  • Line 1 declares the function without parameter and without return value.
  • Line 2 defines the x integer variable, used as array index in the FOR loop.
  • Line 3 initializes the total variable to zero.
  • Lines 4 to 8 implement the FOR loop to scan the rows of the orditems dynamic array.
  • Line 5 adds the value of the current order line to the total. Note the use of the NVL() operator: This NVL() expression will return to zero, if the line_total field is NULL. Adding NULL the the order_total variable would result to NULL.