Example: dispcust.4gl (function query_cust)

This function retrieves a row from the customer table and displays it in a form.

Function query_cust:
01 FUNCTION query_cust() RETURNS ()      -- displays one row 
02   DEFINE l_custrec RECORD
03      store_num    LIKE customer.store_num,
04      store_name   LIKE customer.store_name,
05      addr         LIKE customer.addr,
06      addr2        LIKE customer.addr2,
07      city         LIKE customer.city,
08      state        LIKE customer.state,
09      zip_code      LIKE customer.zip_code,
10      contact_name LIKE customer.contact_name,
11      phone        LIKE customer.phone 
12     END RECORD
13
14   SELECT store_num, 
15          store_name, 
16          addr,
17          addr2, 
18          city, 
19          state, 
20          zip_code,
21          contact_name, 
22          phone  
23     INTO l_custrec.* 
24     FROM customer 
25     WHERE store_num = 101
26     
27   DISPLAY BY NAME l_custrec.* 
28   MESSAGE "Customer " || l_custrec.store_num ||
29          " displayed."
30 END FUNCTION
Note:
  • Line 01 is the beginning of the function query_cust. No variables are passed to the function.
  • Lines 02 thru 12 DEFINE a record l_custrec as LIKE columns in the customer database table, listing each variable separately.
  • Line 14 thru 25 SELECT ... INTO can be used, since the statement will retrieve only one row from the database. The SELECT statement lists each column name to be retrieved, rather than using SELECT *. This allows for the possibility that additional columns might be added to a table at a future date. Since the SELECT list retrieves values for all the variables in the program record, in the order listed in the DEFINE statement, the shorthand INTO l_custrec.* can be used.
  • Line 27 The names in the program record l_custrec match the names of screen fields on the form, so DISPLAY BY NAME can be used. l_custrec.* indicates that all of the members of the program record are to be displayed.
  • Lines 28 and 29 A string for the MESSAGE statement is concatenated together using the double pipe ( || ) operator and displayed. The message consists of the string "Customer", the value of l_custrec.store_num, and the string "displayed".

There are no additional statements in the function, so the program returns to the MENU statement, awaiting the user's next action.